Skip to content

Commit b5fa5d5

Browse files
committed
Working on better and extendable Editors
1 parent 15eba1c commit b5fa5d5

10 files changed

Lines changed: 183 additions & 56 deletions
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
using UnityEditor;
2+
3+
namespace Toolbox.Editor
4+
{
5+
//TODO: new name
6+
public class BasicToolboxEditor : IToolboxEditor
7+
{
8+
public UnityEditor.Editor ContextEditor => throw new System.NotImplementedException();
9+
10+
public void DrawCustomProperty(SerializedProperty property)
11+
{
12+
ToolboxEditorGui.DrawToolboxProperty(property);
13+
}
14+
15+
public void DrawCustomInspector()
16+
{
17+
DrawCustomInspector(ContextEditor.serializedObject);
18+
}
19+
20+
public void DrawCustomInspector(SerializedObject serializedObject)
21+
{
22+
if (!ToolboxDrawerModule.ToolboxDrawersAllowed)
23+
{
24+
ContextEditor.DrawDefaultInspector();
25+
//DrawDefaultInspector();
26+
return;
27+
}
28+
29+
serializedObject.Update();
30+
var property = serializedObject.GetIterator();
31+
//enter to the 'Base' property
32+
if (property.NextVisible(true))
33+
{
34+
var isScript = PropertyUtility.IsDefaultScriptProperty(property);
35+
//try to draw the first property (m_Script)
36+
using (new EditorGUI.DisabledScope(isScript))
37+
{
38+
DrawCustomProperty(property.Copy());
39+
}
40+
41+
//iterate over all other serialized properties
42+
//NOTE: every child will be handled internally
43+
while (property.NextVisible(false))
44+
{
45+
DrawCustomProperty(property.Copy());
46+
}
47+
}
48+
49+
serializedObject.ApplyModifiedProperties();
50+
}
51+
}
52+
}

Assets/Editor Toolbox/Editor/BasicToolboxEditor.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Editor Toolbox/Editor/Editors.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using UnityEditor.AssetImporters;
2+
3+
namespace Toolbox.Editor.Editors
4+
{
5+
public class ToolboxScriptedImporterEditor : ScriptedImporterEditor
6+
{
7+
//TODO:
8+
}
9+
}

Assets/Editor Toolbox/Editor/Editors/ToolboxScriptedImporterEditor.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using UnityEditor;
2+
3+
namespace Toolbox.Editor
4+
{
5+
using Editor = UnityEditor.Editor;
6+
7+
public interface IToolboxEditor
8+
{
9+
void DrawCustomProperty(SerializedProperty property);
10+
void DrawCustomInspector();
11+
void DrawCustomInspector(SerializedObject serializedObject);
12+
13+
Editor ContextEditor { get; }
14+
}
15+
}

Assets/Editor Toolbox/Editor/IToolboxEditor.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Editor Toolbox/Editor/ToolboxEditor.cs

Lines changed: 17 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -13,32 +13,19 @@ namespace Toolbox.Editor
1313
/// </summary>
1414
[CustomEditor(typeof(Object), true, isFallback = true)]
1515
[CanEditMultipleObjects]
16-
public class ToolboxEditor : Editor
16+
public class ToolboxEditor : Editor, IToolboxEditor
1717
{
18+
private readonly IToolboxEditor nestedEditor = new BasicToolboxEditor();
19+
1820
private readonly HashSet<string> propertiesToIgnore = new HashSet<string>();
1921

2022

2123
/// <summary>
2224
/// Inspector GUI re-draw call.
2325
/// </summary>
24-
public override sealed void OnInspectorGUI()
26+
public sealed override void OnInspectorGUI()
2527
{
26-
try
27-
{
28-
OnBeginToolboxEditor?.Invoke(this);
29-
DrawCustomInspector();
30-
}
31-
catch (Exception)
32-
{
33-
//make sure to catch all Exceptions (especially ExitGUIException),
34-
//it will allow us to safely dispose all layout-based controls, etc.
35-
OnBreakToolboxEditor?.Invoke(this);
36-
throw;
37-
}
38-
finally
39-
{
40-
OnCloseToolboxEditor?.Invoke(this);
41-
}
28+
ToolboxEditorHandler.HandleToolboxEditor(this);
4229
}
4330

4431
/// <summary>
@@ -47,51 +34,23 @@ public override sealed void OnInspectorGUI()
4734
/// <param name="property">Property to display.</param>
4835
public virtual void DrawCustomProperty(SerializedProperty property)
4936
{
50-
var propertyPath = property.propertyPath;
51-
if (propertiesToIgnore.Contains(propertyPath))
52-
{
53-
return;
54-
}
55-
56-
ToolboxEditorGui.DrawToolboxProperty(property);
37+
nestedEditor.DrawCustomProperty(property);
5738
}
5839

5940
/// <summary>
60-
/// Draws each available property using internal <see cref="Drawers.ToolboxDrawer"/>s.
41+
/// Draws each available property using internally <see cref="Drawers.ToolboxDrawer"/>s.
6142
/// </summary>
6243
public virtual void DrawCustomInspector()
6344
{
64-
if (ToolboxDrawerModule.ToolboxDrawersAllowed)
65-
{
66-
serializedObject.Update();
67-
68-
var isExpanded = true;
69-
var property = serializedObject.GetIterator();
70-
//enter to the 'Base' property
71-
if (property.NextVisible(isExpanded))
72-
{
73-
isExpanded = false;
74-
var script = PropertyUtility.IsDefaultScriptProperty(property);
75-
76-
//try to draw the first property (m_Script)
77-
using (new EditorGUI.DisabledScope(script))
78-
{
79-
DrawCustomProperty(property.Copy());
80-
}
81-
82-
//iterate over all other serialized properties
83-
//NOTE: every child will be handled internally
84-
while (property.NextVisible(isExpanded))
85-
{
86-
DrawCustomProperty(property.Copy());
87-
}
88-
}
89-
90-
serializedObject.ApplyModifiedProperties();
91-
return;
92-
}
45+
nestedEditor.DrawCustomInspector();
46+
}
9347

94-
DrawDefaultInspector();
48+
/// <summary>
49+
/// Draws each available property using internally <see cref="Drawers.ToolboxDrawer"/>s.
50+
/// </summary>
51+
public virtual void DrawCustomInspector(SerializedObject serializedObject)
52+
{
53+
nestedEditor.DrawCustomInspector(serializedObject);
9554
}
9655

9756
/// <summary>
@@ -114,5 +73,7 @@ public void IgnoreProperty(string propertyPath)
11473
public static event Action<Editor> OnBeginToolboxEditor;
11574
public static event Action<Editor> OnBreakToolboxEditor;
11675
public static event Action<Editor> OnCloseToolboxEditor;
76+
77+
Editor IToolboxEditor.ContextEditor => this;
11778
}
11879
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using System;
2+
3+
namespace Toolbox.Editor
4+
{
5+
using Editor = UnityEditor.Editor;
6+
7+
internal static class ToolboxEditorHandler
8+
{
9+
public static void HandleToolboxEditor(IToolboxEditor editor)
10+
{
11+
try
12+
{
13+
ContextEditor = editor.ContextEditor;
14+
OnBeginToolboxEditor?.Invoke(ContextEditor);
15+
editor.DrawCustomInspector();
16+
}
17+
catch (Exception)
18+
{
19+
//make sure to catch all Exceptions (especially ExitGUIException),
20+
//it will allow us to safely dispose all layout-based controls, etc.
21+
OnBreakToolboxEditor?.Invoke(ContextEditor);
22+
throw;
23+
}
24+
finally
25+
{
26+
OnCloseToolboxEditor?.Invoke(ContextEditor);
27+
ContextEditor = null;
28+
}
29+
}
30+
31+
32+
public static event Action<Editor> OnBeginToolboxEditor;
33+
public static event Action<Editor> OnBreakToolboxEditor;
34+
public static event Action<Editor> OnCloseToolboxEditor;
35+
36+
public static Editor ContextEditor { get; private set; }
37+
}
38+
}

Assets/Editor Toolbox/Editor/ToolboxEditorHandler.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)