From 39083086113b0d2771a7308363c00ee40d90470b Mon Sep 17 00:00:00 2001 From: VladV Date: Thu, 11 May 2023 09:59:24 +0400 Subject: [PATCH] Rework editors (Fix #114) --- Editor/Editors/TriEditor.cs | 78 +++++++ Editor/{ => Editors}/TriEditor.cs.meta | 0 Editor/Editors/TriMonoBehaviourEditor.cs | 11 + Editor/Editors/TriMonoBehaviourEditor.cs.meta | 3 + Editor/Editors/TriScriptableObjectEditor.cs | 11 + .../Editors/TriScriptableObjectEditor.cs.meta | 3 + Editor/Editors/TriScriptedImporterEditor.cs | 32 +++ .../Editors/TriScriptedImporterEditor.cs.meta | 3 + Editor/TriEditor.cs | 195 ------------------ .../EditorProxy.cs | 12 ++ .../EditorProxy.cs.meta | 3 + 11 files changed, 156 insertions(+), 195 deletions(-) create mode 100644 Editor/Editors/TriEditor.cs rename Editor/{ => Editors}/TriEditor.cs.meta (100%) create mode 100644 Editor/Editors/TriMonoBehaviourEditor.cs create mode 100644 Editor/Editors/TriMonoBehaviourEditor.cs.meta create mode 100644 Editor/Editors/TriScriptableObjectEditor.cs create mode 100644 Editor/Editors/TriScriptableObjectEditor.cs.meta create mode 100644 Editor/Editors/TriScriptedImporterEditor.cs create mode 100644 Editor/Editors/TriScriptedImporterEditor.cs.meta delete mode 100644 Editor/TriEditor.cs create mode 100644 Unity.InternalAPIEditorBridge.012/EditorProxy.cs create mode 100644 Unity.InternalAPIEditorBridge.012/EditorProxy.cs.meta diff --git a/Editor/Editors/TriEditor.cs b/Editor/Editors/TriEditor.cs new file mode 100644 index 0000000..9134ba0 --- /dev/null +++ b/Editor/Editors/TriEditor.cs @@ -0,0 +1,78 @@ +using TriInspector.Utilities; +using UnityEditor; +using UnityEngine; + +namespace TriInspector.Editors +{ + public abstract class TriEditor : Editor + { + private TriPropertyTreeForSerializedObject _inspector; + + private void OnDisable() + { + OnDisable(this, ref _inspector); + } + + public override void OnInspectorGUI() + { + OnInspectorGUI(this, ref _inspector); + } + + public static void OnDisable(Editor editor, ref TriPropertyTreeForSerializedObject inspector) + { + inspector?.Dispose(); + inspector = null; + } + + public static void OnInspectorGUI(Editor editor, + ref TriPropertyTreeForSerializedObject inspector) + { + var serializedObject = editor.serializedObject; + + if (serializedObject.targetObjects.Length == 0) + { + return; + } + + if (serializedObject.targetObject == null) + { + EditorGUILayout.HelpBox("Script is missing", MessageType.Warning); + return; + } + + foreach (var targetObject in serializedObject.targetObjects) + { + if (TriGuiHelper.IsEditorTargetPushed(targetObject)) + { + GUILayout.Label("Recursive inline editors not supported"); + return; + } + } + + if (inspector == null) + { + inspector = new TriPropertyTreeForSerializedObject(serializedObject); + } + + serializedObject.UpdateIfRequiredOrScript(); + + inspector.Update(); + inspector.RunValidationIfRequired(); + + using (TriGuiHelper.PushEditorTarget(serializedObject.targetObject)) + { + inspector.Draw(); + } + + if (serializedObject.ApplyModifiedProperties()) + { + inspector.RequestValidation(); + } + + if (inspector.RepaintRequired) + { + editor.Repaint(); + } + } + } +} \ No newline at end of file diff --git a/Editor/TriEditor.cs.meta b/Editor/Editors/TriEditor.cs.meta similarity index 100% rename from Editor/TriEditor.cs.meta rename to Editor/Editors/TriEditor.cs.meta diff --git a/Editor/Editors/TriMonoBehaviourEditor.cs b/Editor/Editors/TriMonoBehaviourEditor.cs new file mode 100644 index 0000000..5d178e1 --- /dev/null +++ b/Editor/Editors/TriMonoBehaviourEditor.cs @@ -0,0 +1,11 @@ +using UnityEditor; +using UnityEngine; + +namespace TriInspector.Editors +{ + [CanEditMultipleObjects] + [CustomEditor(typeof(MonoBehaviour), editorForChildClasses: true, isFallback = true)] + internal sealed class TriMonoBehaviourEditor : TriEditor + { + } +} \ No newline at end of file diff --git a/Editor/Editors/TriMonoBehaviourEditor.cs.meta b/Editor/Editors/TriMonoBehaviourEditor.cs.meta new file mode 100644 index 0000000..7496852 --- /dev/null +++ b/Editor/Editors/TriMonoBehaviourEditor.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: ebc893dd0ca44a789a00c03f7a71dc56 +timeCreated: 1683784191 \ No newline at end of file diff --git a/Editor/Editors/TriScriptableObjectEditor.cs b/Editor/Editors/TriScriptableObjectEditor.cs new file mode 100644 index 0000000..4ecb52c --- /dev/null +++ b/Editor/Editors/TriScriptableObjectEditor.cs @@ -0,0 +1,11 @@ +using UnityEditor; +using UnityEngine; + +namespace TriInspector.Editors +{ + [CanEditMultipleObjects] + [CustomEditor(typeof(ScriptableObject), editorForChildClasses: true, isFallback = true)] + internal sealed class TriScriptableObjectEditor : TriEditor + { + } +} \ No newline at end of file diff --git a/Editor/Editors/TriScriptableObjectEditor.cs.meta b/Editor/Editors/TriScriptableObjectEditor.cs.meta new file mode 100644 index 0000000..fc7e757 --- /dev/null +++ b/Editor/Editors/TriScriptableObjectEditor.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 5fe6a72a9e734fcc8ab5f29cd13c9d53 +timeCreated: 1683784197 \ No newline at end of file diff --git a/Editor/Editors/TriScriptedImporterEditor.cs b/Editor/Editors/TriScriptedImporterEditor.cs new file mode 100644 index 0000000..1b775fa --- /dev/null +++ b/Editor/Editors/TriScriptedImporterEditor.cs @@ -0,0 +1,32 @@ +using TriInspectorUnityInternalBridge; +using UnityEditor; +using UnityEditor.AssetImporters; + +namespace TriInspector.Editors +{ + [CanEditMultipleObjects] + [CustomEditor(typeof(ScriptedImporter), editorForChildClasses: true)] + public sealed class TriScriptedImporterEditor : ScriptedImporterEditor + { + private TriPropertyTreeForSerializedObject _inspector; + + public override void OnDisable() + { + TriEditor.OnDisable(this, ref _inspector); + + base.OnDisable(); + } + + public override void OnInspectorGUI() + { + TriEditor.OnInspectorGUI(this, ref _inspector); + + if (extraDataType != null) + { + EditorProxy.DoDrawDefaultInspector(extraDataSerializedObject); + } + + ApplyRevertGUI(); + } + } +} \ No newline at end of file diff --git a/Editor/Editors/TriScriptedImporterEditor.cs.meta b/Editor/Editors/TriScriptedImporterEditor.cs.meta new file mode 100644 index 0000000..ba1efb4 --- /dev/null +++ b/Editor/Editors/TriScriptedImporterEditor.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 421d7c27350d45308b6a91b76560bb50 +timeCreated: 1683784201 \ No newline at end of file diff --git a/Editor/TriEditor.cs b/Editor/TriEditor.cs deleted file mode 100644 index dc3f88b..0000000 --- a/Editor/TriEditor.cs +++ /dev/null @@ -1,195 +0,0 @@ -using TriInspector.Utilities; -using UnityEditor; -using UnityEditor.AssetImporters; -using UnityEngine; - -namespace TriInspector -{ - [CanEditMultipleObjects] - [CustomEditor(typeof(MonoBehaviour), editorForChildClasses: true, isFallback = true)] - internal sealed class TriMonoBehaviourEditor : TriEditor - { - } - - [CanEditMultipleObjects] - [CustomEditor(typeof(ScriptableObject), editorForChildClasses: true, isFallback = true)] - internal sealed class TriScriptableObjectEditor : TriEditor - { - } - - [CanEditMultipleObjects] - [CustomEditor(typeof(AssetImporter), editorForChildClasses: true)] - public sealed class TriAssetImporterEditor : AssetImporterEditor - { - private TriPropertyTreeForSerializedObject _inspector; - - public override void OnDisable() - { - base.OnDisable(); - _inspector?.Dispose(); - _inspector = null; - } - - public override void OnInspectorGUI() - { - if (serializedObject.targetObjects.Length == 0) - { - return; - } - - if (serializedObject.targetObject == null) - { - EditorGUILayout.HelpBox("Script is missing", MessageType.Warning); - return; - } - - if (TriGuiHelper.IsEditorTargetPushed(serializedObject.targetObject)) - { - GUILayout.Label("Recursive inline editors not supported"); - return; - } - - if (_inspector == null) - { - _inspector = new TriPropertyTreeForSerializedObject(serializedObject); - } - - serializedObject.UpdateIfRequiredOrScript(); - - _inspector.Update(); - _inspector.RunValidationIfRequired(); - - using (TriGuiHelper.PushEditorTarget(target)) - { - _inspector.Draw(); - ApplyRevertGUI(); - } - - if (serializedObject.ApplyModifiedProperties()) - { - _inspector.RequestValidation(); - } - - if (_inspector.RepaintRequired) - { - Repaint(); - } - } - } - - [CanEditMultipleObjects] - [CustomEditor(typeof(ScriptedImporter), editorForChildClasses: true)] - public sealed class TriScriptedImporterEditor : ScriptedImporterEditor - { - private TriPropertyTreeForSerializedObject _inspector; - - public override void OnDisable() - { - base.OnDisable(); - _inspector?.Dispose(); - _inspector = null; - } - - public override void OnInspectorGUI() - { - if (serializedObject.targetObjects.Length == 0) - { - return; - } - - if (serializedObject.targetObject == null) - { - EditorGUILayout.HelpBox("Script is missing", MessageType.Warning); - return; - } - - if (TriGuiHelper.IsEditorTargetPushed(serializedObject.targetObject)) - { - GUILayout.Label("Recursive inline editors not supported"); - return; - } - - if (_inspector == null) - { - _inspector = new TriPropertyTreeForSerializedObject(serializedObject); - } - - serializedObject.UpdateIfRequiredOrScript(); - - _inspector.Update(); - _inspector.RunValidationIfRequired(); - - using (TriGuiHelper.PushEditorTarget(target)) - { - _inspector.Draw(); - ApplyRevertGUI(); - } - - if (serializedObject.ApplyModifiedProperties()) - { - _inspector.RequestValidation(); - } - - if (_inspector.RepaintRequired) - { - Repaint(); - } - } - } - - public class TriEditor : Editor - { - private TriPropertyTreeForSerializedObject _inspector; - - private void OnDisable() - { - _inspector?.Dispose(); - _inspector = null; - } - - public override void OnInspectorGUI() - { - if (serializedObject.targetObjects.Length == 0) - { - return; - } - - if (serializedObject.targetObject == null) - { - EditorGUILayout.HelpBox("Script is missing", MessageType.Warning); - return; - } - - if (TriGuiHelper.IsEditorTargetPushed(serializedObject.targetObject)) - { - GUILayout.Label("Recursive inline editors not supported"); - return; - } - - if (_inspector == null) - { - _inspector = new TriPropertyTreeForSerializedObject(serializedObject); - } - - serializedObject.UpdateIfRequiredOrScript(); - - _inspector.Update(); - _inspector.RunValidationIfRequired(); - - using (TriGuiHelper.PushEditorTarget(target)) - { - _inspector.Draw(); - } - - if (serializedObject.ApplyModifiedProperties()) - { - _inspector.RequestValidation(); - } - - if (_inspector.RepaintRequired) - { - Repaint(); - } - } - } -} \ No newline at end of file diff --git a/Unity.InternalAPIEditorBridge.012/EditorProxy.cs b/Unity.InternalAPIEditorBridge.012/EditorProxy.cs new file mode 100644 index 0000000..006d9fe --- /dev/null +++ b/Unity.InternalAPIEditorBridge.012/EditorProxy.cs @@ -0,0 +1,12 @@ +using UnityEditor; + +namespace TriInspectorUnityInternalBridge +{ + internal static class EditorProxy + { + public static void DoDrawDefaultInspector(SerializedObject obj) + { + Editor.DoDrawDefaultInspector(obj); + } + } +} \ No newline at end of file diff --git a/Unity.InternalAPIEditorBridge.012/EditorProxy.cs.meta b/Unity.InternalAPIEditorBridge.012/EditorProxy.cs.meta new file mode 100644 index 0000000..44e792f --- /dev/null +++ b/Unity.InternalAPIEditorBridge.012/EditorProxy.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 993f9dfd66614ba68fcf95c0e7e098e3 +timeCreated: 1683783433 \ No newline at end of file