From 8ef38178e6e16e52c823275fd048c4bbd838752f Mon Sep 17 00:00:00 2001
From: Menotdan <32620310+Menotdan@users.noreply.github.com>
Date: Wed, 10 Apr 2024 18:13:27 -0400
Subject: [PATCH] Allow the user to pick prefab type upon creation of a prefab.
---
.../Content/Create/PrefabCreateEntry.cs | 72 +++++++++++++++++++
Source/Editor/Content/Proxy/PrefabProxy.cs | 16 ++---
2 files changed, 76 insertions(+), 12 deletions(-)
create mode 100644 Source/Editor/Content/Create/PrefabCreateEntry.cs
diff --git a/Source/Editor/Content/Create/PrefabCreateEntry.cs b/Source/Editor/Content/Create/PrefabCreateEntry.cs
new file mode 100644
index 000000000..b307f09dd
--- /dev/null
+++ b/Source/Editor/Content/Create/PrefabCreateEntry.cs
@@ -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
+{
+ ///
+ /// 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);
+ }
+ }
+}
diff --git a/Source/Editor/Content/Proxy/PrefabProxy.cs b/Source/Editor/Content/Proxy/PrefabProxy.cs
index 18860995e..a97b6232f 100644
--- a/Source/Editor/Content/Proxy/PrefabProxy.cs
+++ b/Source/Editor/Content/Proxy/PrefabProxy.cs
@@ -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
///
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;
}
///