// Copyright (c) 2012-2024 Wojciech Figat. All rights reserved.
using System;
using FlaxEditor.Scripting;
using FlaxEngine;
using Object = FlaxEngine.Object;
namespace FlaxEditor.Content.Create
{
///
/// Visual Script asset creating handler. Allows to specify base class to inherit from.
///
///
public class PrefabCreateEntry : CreateFileEntry
{
///
/// The create options.
///
public class Options
{
///
/// The root actor.
///
[TypeReference(typeof(FlaxEngine.Actor), nameof(IsValid))]
[Tooltip("The actor type of the root of the new Prefab.")]
public Type RootActorType = typeof(EmptyActor);
private static bool IsValid(Type type)
{
return (type.IsPublic || type.IsNestedPublic) && !type.IsAbstract && !type.IsGenericType;
}
}
private readonly Options _options = new Options();
///
public override object Settings => _options;
///
/// Initializes a new instance of the class.
///
/// The result file url.
public PrefabCreateEntry(string resultUrl)
: base("Settings", resultUrl)
{
}
///
public override bool Create()
{
if (_options.RootActorType == null)
_options.RootActorType = typeof(EmptyActor);
ScriptType actorType = new ScriptType(_options.RootActorType);
Actor actor = null;
try
{
actor = actorType.CreateInstance() as Actor;
Object.Destroy(actor, 20.0f);
}
catch (Exception ex)
{
Editor.LogError("Failed to create prefab with root actor type: " + actorType.Name);
Editor.LogWarning(ex);
return true;
}
return PrefabManager.CreatePrefab(actor, ResultUrl, true);
}
}
}