Allow the user to pick prefab type upon creation of a prefab.
This commit is contained in:
72
Source/Editor/Content/Create/PrefabCreateEntry.cs
Normal file
72
Source/Editor/Content/Create/PrefabCreateEntry.cs
Normal file
@@ -0,0 +1,72 @@
|
||||
// Copyright (c) 2012-2024 Wojciech Figat. All rights reserved.
|
||||
|
||||
using System;
|
||||
using FlaxEditor.Scripting;
|
||||
using FlaxEngine;
|
||||
using Object = FlaxEngine.Object;
|
||||
|
||||
namespace FlaxEditor.Content.Create
|
||||
{
|
||||
/// <summary>
|
||||
/// Visual Script asset creating handler. Allows to specify base class to inherit from.
|
||||
/// </summary>
|
||||
/// <seealso cref="FlaxEditor.Content.Create.CreateFileEntry" />
|
||||
public class PrefabCreateEntry : CreateFileEntry
|
||||
{
|
||||
/// <summary>
|
||||
/// The create options.
|
||||
/// </summary>
|
||||
public class Options
|
||||
{
|
||||
/// <summary>
|
||||
/// The root actor.
|
||||
/// </summary>
|
||||
[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();
|
||||
|
||||
/// <inheritdoc />
|
||||
public override object Settings => _options;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="PrefabCreateEntry"/> class.
|
||||
/// </summary>
|
||||
/// <param name="resultUrl">The result file url.</param>
|
||||
public PrefabCreateEntry(string resultUrl)
|
||||
: base("Settings", resultUrl)
|
||||
{
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
using System;
|
||||
using System.IO;
|
||||
using FlaxEditor.Content.Create;
|
||||
using FlaxEditor.Content.Thumbnails;
|
||||
using FlaxEditor.Viewport.Previews;
|
||||
using FlaxEditor.Windows;
|
||||
@@ -76,30 +77,21 @@ namespace FlaxEditor.Content
|
||||
/// <inheritdoc />
|
||||
public override void Create(string outputPath, object arg)
|
||||
{
|
||||
bool resetTransform = false;
|
||||
var transform = Transform.Identity;
|
||||
if (!(arg is Actor actor))
|
||||
{
|
||||
// Create default prefab root object
|
||||
actor = new EmptyActor
|
||||
{
|
||||
Name = "Root"
|
||||
};
|
||||
|
||||
// Cleanup it after usage
|
||||
Object.Destroy(actor, 20.0f);
|
||||
Editor.Instance.ContentImporting.Create(new PrefabCreateEntry(outputPath));
|
||||
return;
|
||||
}
|
||||
else if (actor.Scene != null)
|
||||
{
|
||||
// Create prefab with identity transform so the actor instance on a level will have it customized
|
||||
resetTransform = true;
|
||||
transform = actor.LocalTransform;
|
||||
actor.LocalTransform = Transform.Identity;
|
||||
}
|
||||
|
||||
PrefabManager.CreatePrefab(actor, outputPath, true);
|
||||
if (resetTransform)
|
||||
actor.LocalTransform = transform;
|
||||
actor.LocalTransform = transform;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
|
||||
Reference in New Issue
Block a user