// Copyright (c) 2012-2021 Wojciech Figat. All rights reserved.
using FlaxEngine;
using FlaxEngine.GUI;
using Object = FlaxEngine.Object;
namespace FlaxEditor.Viewport.Previews
{
///
/// Prefab asset preview editor viewport.
///
///
public class PrefabPreview : AssetPreview
{
///
/// The preview that is during prefab instance spawning. Used to link some actors such as UIControl to preview scene and view.
///
internal static PrefabPreview LoadingPreview;
private Prefab _prefab;
private Actor _instance;
internal Control customControlLinked;
///
/// Gets or sets the prefab asset to preview.
///
public Prefab Prefab
{
get => _prefab;
set
{
if (_prefab != value)
{
if (_instance)
{
if (customControlLinked != null)
{
customControlLinked.Parent = null;
customControlLinked = null;
}
Task.RemoveCustomActor(_instance);
Object.Destroy(_instance);
}
_prefab = value;
if (_prefab)
{
_prefab.WaitForLoaded(); // TODO: use lazy prefab spawning to reduce stalls
var prevPreview = LoadingPreview;
LoadingPreview = this;
_instance = PrefabManager.SpawnPrefab(_prefab, null);
LoadingPreview = prevPreview;
if (_instance == null)
{
_prefab = null;
throw new FlaxException("Failed to spawn a prefab for the preview.");
}
Task.AddCustomActor(_instance);
}
}
}
}
///
/// Gets the instance of the prefab spawned for the preview.
///
public Actor Instance
{
get => _instance;
internal set => _instance = value;
}
///
/// Initializes a new instance of the class.
///
/// if set to true use widgets.
public PrefabPreview(bool useWidgets)
: base(useWidgets)
{
}
///
public override void OnDestroy()
{
// Cleanup
Prefab = null;
base.OnDestroy();
}
}
}