From d9e8b1c51bf9bff4bc4d30078fc67dbc4bd7e490 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mi=C5=82osz=20Matkowski?= Date: Sat, 27 Jan 2024 20:04:48 +0100 Subject: [PATCH] Minor UX improvements in the ScriptableObjectCreationWizard --- .../Wizards/ScriptableObjectCreationWizard.cs | 58 ++++++++++++++----- 1 file changed, 45 insertions(+), 13 deletions(-) diff --git a/Assets/Editor Toolbox/Editor/Wizards/ScriptableObjectCreationWizard.cs b/Assets/Editor Toolbox/Editor/Wizards/ScriptableObjectCreationWizard.cs index 4d288fb0..9295e08b 100644 --- a/Assets/Editor Toolbox/Editor/Wizards/ScriptableObjectCreationWizard.cs +++ b/Assets/Editor Toolbox/Editor/Wizards/ScriptableObjectCreationWizard.cs @@ -9,6 +9,7 @@ namespace Toolbox.Editor.Wizards { using Toolbox.Editor.Internal; + using Editor = UnityEditor.Editor; /// /// Utility window responsible for creation of s. @@ -34,7 +35,7 @@ private class CreationData { private bool IsDefaultObjectValid() { - return DefaultObject != null && DefaultObject.GetType() == InstanceType; + return BlueprintObject != null && BlueprintObject.GetType() == InstanceType; } public void Validate() @@ -47,7 +48,7 @@ public void Validate() InstancesCount = Mathf.Max(InstancesCount, 1); if (!IsDefaultObjectValid()) { - DefaultObject = null; + BlueprintObject = null; } } @@ -59,7 +60,7 @@ public void Validate() public int InstancesCount { get; set; } = 1; [field: SerializeField, InLineEditor] [field: Tooltip("Will be used as a blueprint for all created ScriptableObjects.")] - public Object DefaultObject { get; set; } + public Object BlueprintObject { get; set; } } private static readonly TypeConstraintContext sharedConstraint = new TypeConstraintScriptableObject(); @@ -69,7 +70,13 @@ public void Validate() private readonly CreationData data = new CreationData(); private bool inspectDefaultObject; - private bool useSearchField = true; + private Editor blueprintObjectEditor; + + protected override void OnDestroy() + { + base.OnDestroy(); + DestroyImmediate(blueprintObjectEditor); + } [MenuItem("Assets/Create/Editor Toolbox/Wizards/ScriptableObject Creation Wizard", priority = 5)] internal static void Initialize() @@ -83,10 +90,8 @@ private void DrawSettingsPanel() { EditorGUILayout.LabelField("Settings", EditorStyles.boldLabel); - useSearchField = EditorGUILayout.ToggleLeft("Use Search Field", useSearchField); - var rect = EditorGUILayout.GetControlRect(true); - typeField.OnGui(rect, useSearchField, OnTypeSelected, data.InstanceType); + typeField.OnGui(rect, true, OnTypeSelected, data.InstanceType); if (data.InstanceType == null) { return; @@ -97,7 +102,15 @@ private void DrawSettingsPanel() EditorGUI.BeginChangeCheck(); data.InstanceName = EditorGUILayout.TextField(Style.nameContent, data.InstanceName); data.InstancesCount = EditorGUILayout.IntField(Style.countContent, data.InstancesCount); - var assignedInstance = EditorGUILayout.ObjectField(Style.objectContent, data.DefaultObject, data.InstanceType, false); + + EditorGUI.BeginChangeCheck(); + var assignedInstance = EditorGUILayout.ObjectField(Style.objectContent, data.BlueprintObject, data.InstanceType, false); + data.BlueprintObject = assignedInstance; + if (EditorGUI.EndChangeCheck()) + { + UpdateBlueprintObjectEditor(); + } + if (assignedInstance != null) { inspectDefaultObject = GUILayout.Toggle(inspectDefaultObject, @@ -112,11 +125,11 @@ private void DrawSettingsPanel() { using (new EditorGUILayout.VerticalScope(Style.backgroundStyle)) { - ToolboxEditorGui.DrawObjectProperties(assignedInstance); + blueprintObjectEditor.OnInspectorGUI(); } } - data.DefaultObject = assignedInstance; + if (EditorGUI.EndChangeCheck()) { OnWizardUpdate(); @@ -147,7 +160,7 @@ private void CreateObjects(CreationData data) var instancesCount = data.InstancesCount; for (var i = 0; i < instancesCount; i++) { - var instance = CreateObject(data.InstanceType, data.DefaultObject); + var instance = CreateObject(data.InstanceType, data.BlueprintObject); CreateAsset(instance, data.InstanceName, assetPath, i); } @@ -181,6 +194,25 @@ private void OnTypeSelected(Type type) } } + private void UpdateBlueprintObjectEditor() + { + DestroyImmediate(blueprintObjectEditor); + blueprintObjectEditor = null; + + var targetObject = data.BlueprintObject; + if (targetObject == null) + { + return; + } + + blueprintObjectEditor = Editor.CreateEditor(targetObject); + blueprintObjectEditor.hideFlags = HideFlags.HideAndDontSave; + if (blueprintObjectEditor is ToolboxEditor toolboxEditor) + { + toolboxEditor.IgnoreProperty(PropertyUtility.Defaults.scriptPropertyName); + } + } + private static string GetActiveFolderPath() { var projectWindowUtilType = typeof(ProjectWindowUtil); @@ -219,8 +251,8 @@ private static class Style internal static readonly GUIStyle foldoutStyle; internal static readonly GUIContent nameContent = new GUIContent("Instance Name"); - internal static readonly GUIContent countContent = new GUIContent("Instances To Create", "Indicates how many instances will be created."); - internal static readonly GUIContent objectContent = new GUIContent("Default Object", "Will be used as a blueprint for all created ScriptableObjects."); + internal static readonly GUIContent countContent = new GUIContent("Instances Count", "Indicates how many instances will be created."); + internal static readonly GUIContent objectContent = new GUIContent("Blueprint Object", "Will be used as a blueprint for all created ScriptableObjects."); internal static readonly GUIContent foldoutContent = new GUIContent("Inspect", "Show/Hide Properties"); internal static readonly GUILayoutOption[] foldoutOptions = new GUILayoutOption[]