Skip to content

Commit deb3358

Browse files
committed
Minor refactor changes & sample Editor for ScriptedImporters
1 parent 4e87dda commit deb3358

11 files changed

Lines changed: 127 additions & 63 deletions

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,25 @@ public sealed override void OnInspectorGUI()
1414

1515
public virtual void DrawCustomInspector()
1616
{
17-
Drawer.DrawToolboxInspector(serializedObject);
17+
Drawer.DrawEditor(serializedObject);
1818
if (extraDataType != null)
1919
{
20-
Drawer.DrawToolboxInspector(extraDataSerializedObject);
20+
Drawer.DrawEditor(extraDataSerializedObject);
2121
}
2222

2323
ApplyRevertGUI();
2424
}
2525

26-
//TODO: ignore properties
2726
public void IgnoreProperty(SerializedProperty property)
28-
{ }
27+
{
28+
Drawer.IgnoreProperty(property);
29+
}
2930

3031
public void IgnoreProperty(string propertyPath)
31-
{ }
32+
{
33+
Drawer.IgnoreProperty(propertyPath);
34+
}
35+
3236

3337
Editor IToolboxEditor.ContextEditor => this;
3438
public IToolboxEditorDrawer Drawer { get; } = new ToolboxEditorDrawer();

Assets/Editor Toolbox/Editor/IToolboxEditorDrawer.cs

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,20 @@ namespace Toolbox.Editor
44
{
55
public interface IToolboxEditorDrawer
66
{
7-
/// <summary>
8-
/// Handles property display process using custom <see cref="Drawers.ToolboxDrawer"/>.
9-
/// </summary>
10-
/// <param name="property">Property to display.</param>
11-
void DrawToolboxProperty(SerializedProperty property);
12-
/// <summary>
13-
/// Handles property display process using the default (built-in) drawers.
14-
/// </summary>
15-
/// <param name="property">Property to display.</param>
16-
void DrawDefaultProperty(SerializedProperty property);
177
/// <summary>
188
/// Draws each available property using internally <see cref="Drawers.ToolboxDrawer"/>s.
199
/// </summary>
20-
void DrawToolboxInspector(SerializedObject serializedObject);
10+
void DrawEditor(SerializedObject serializedObject);
2111
/// <summary>
2212
/// Draws <see cref="SerializedProperty"/>/ies in the default (native) way.
2313
/// </summary>
2414
/// <param name="serializedObject"></param>
25-
void DrawDefaultInspector(SerializedObject serializedObject);
15+
void DrawDefaultEditor(SerializedObject serializedObject);
16+
/// <summary>
17+
/// TODO
18+
/// </summary>
19+
/// <param name="serializedObject"></param>
20+
void DrawToolboxEditor(SerializedObject serializedObject);
2621
/// <summary>
2722
/// Forces provided <see cref="SerializedProperty"/> to be ignored in the drawing process.
2823
/// </summary>

Assets/Editor Toolbox/Editor/ToolboxEditor.cs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System.Collections.Generic;
1+
using System;
22

33
using UnityEditor;
44

@@ -26,13 +26,13 @@ public sealed override void OnInspectorGUI()
2626
/// <inheritdoc />
2727
public virtual void DrawCustomProperty(SerializedProperty property)
2828
{
29-
Drawer.DrawToolboxProperty(property);
29+
//Drawer.DrawProperty(property);
3030
}
3131

3232
/// <inheritdoc />
3333
public virtual void DrawCustomInspector()
3434
{
35-
Drawer.DrawToolboxInspector(serializedObject);
35+
Drawer.DrawEditor(serializedObject);
3636
}
3737

3838
/// <inheritdoc />
@@ -50,5 +50,14 @@ public void IgnoreProperty(string propertyPath)
5050

5151
Editor IToolboxEditor.ContextEditor => this;
5252
public IToolboxEditorDrawer Drawer { get; } = new ToolboxEditorDrawer();
53+
54+
#pragma warning disable 0067
55+
[Obsolete("ToolboxEditorHandler.OnBeginToolboxEditor")]
56+
public static event Action<Editor> OnBeginToolboxEditor;
57+
[Obsolete("ToolboxEditorHandler.OnBreakToolboxEditor")]
58+
public static event Action<Editor> OnBreakToolboxEditor;
59+
[Obsolete("ToolboxEditorHandler.OnCloseToolboxEditor")]
60+
public static event Action<Editor> OnCloseToolboxEditor;
61+
#pragma warning restore 0067
5362
}
5463
}

Assets/Editor Toolbox/Editor/ToolboxEditorDrawer.cs

Lines changed: 36 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -8,67 +8,42 @@ namespace Toolbox.Editor
88
public class ToolboxEditorDrawer : IToolboxEditorDrawer
99
{
1010
private readonly HashSet<string> propertiesToIgnore = new HashSet<string>();
11+
//TODO:
12+
private readonly Action<SerializedProperty> toolboxDrawingAction;
13+
private readonly Action<SerializedProperty> defaultDrawingAction;
1114

12-
//TODO: refactor
13-
/// <inheritdoc />
14-
public void DrawToolboxProperty(SerializedProperty property)
15+
public ToolboxEditorDrawer()
1516
{
16-
var propertyPath = property.propertyPath;
17-
if (propertiesToIgnore.Contains(propertyPath))
18-
{
19-
return;
20-
}
21-
22-
ToolboxEditorGui.DrawToolboxProperty(property);
17+
toolboxDrawingAction = ToolboxEditorGui.DrawToolboxProperty;
18+
defaultDrawingAction = ToolboxEditorGui.DrawNativeProperty;
2319
}
2420

25-
/// <inheritdoc />
26-
public void DrawDefaultProperty(SerializedProperty property)
21+
private void DrawProperty(SerializedProperty property, Action<SerializedProperty> drawingAction)
2722
{
2823
var propertyPath = property.propertyPath;
29-
if (propertiesToIgnore.Contains(propertyPath))
24+
if (IsPropertyIgnored(propertyPath))
3025
{
3126
return;
3227
}
3328

34-
ToolboxEditorGui.DrawNativeProperty(property);
29+
drawingAction(property);
3530
}
3631

3732
/// <inheritdoc />
38-
public void DrawToolboxInspector(SerializedObject serializedObject)
33+
public void DrawEditor(SerializedObject serializedObject)
3934
{
40-
if (!ToolboxDrawerModule.ToolboxDrawersAllowed)
35+
if (ToolboxDrawerModule.ToolboxDrawersAllowed)
4136
{
42-
DrawDefaultInspector(serializedObject);
43-
return;
37+
DrawToolboxEditor(serializedObject);
4438
}
45-
46-
//TODO: how to handle default/toolbox drawers
47-
//Action<SerializedProperty> drawAction = ToolboxDrawerModule.ToolboxDrawersAllowed
48-
// ? (Action<SerializedProperty>)DrawToolboxProperty
49-
// : DrawDefaultProperty;
50-
serializedObject.UpdateIfRequiredOrScript();
51-
var property = serializedObject.GetIterator();
52-
if (property.NextVisible(true))
39+
else
5340
{
54-
var isScript = PropertyUtility.IsDefaultScriptProperty(property);
55-
using (new EditorGUI.DisabledScope(isScript))
56-
{
57-
DrawToolboxProperty(property.Copy());
58-
}
59-
60-
while (property.NextVisible(false))
61-
{
62-
DrawToolboxProperty(property.Copy());
63-
}
41+
DrawDefaultEditor(serializedObject);
6442
}
65-
66-
serializedObject.ApplyModifiedProperties();
6743
}
6844

69-
//TODO:
7045
/// <inheritdoc />
71-
public void DrawDefaultInspector(SerializedObject serializedObject)
46+
public void DrawEditor(SerializedObject serializedObject, Action<SerializedProperty> drawingAction)
7247
{
7348
serializedObject.UpdateIfRequiredOrScript();
7449
var property = serializedObject.GetIterator();
@@ -77,18 +52,30 @@ public void DrawDefaultInspector(SerializedObject serializedObject)
7752
var isScript = PropertyUtility.IsDefaultScriptProperty(property);
7853
using (new EditorGUI.DisabledScope(isScript))
7954
{
80-
EditorGUILayout.PropertyField(property, true);
55+
DrawProperty(property.Copy(), drawingAction);
8156
}
8257

8358
while (property.NextVisible(false))
8459
{
85-
EditorGUILayout.PropertyField(property, true);
60+
DrawProperty(property.Copy(), drawingAction);
8661
}
8762
}
8863

8964
serializedObject.ApplyModifiedProperties();
9065
}
9166

67+
/// <inheritdoc />
68+
public void DrawToolboxEditor(SerializedObject serializedObject)
69+
{
70+
DrawEditor(serializedObject, ToolboxEditorGui.DrawToolboxProperty);
71+
}
72+
73+
/// <inheritdoc />
74+
public void DrawDefaultEditor(SerializedObject serializedObject)
75+
{
76+
DrawEditor(serializedObject, ToolboxEditorGui.DrawNativeProperty);
77+
}
78+
9279
/// <inheritdoc />
9380
public void IgnoreProperty(SerializedProperty property)
9481
{
@@ -100,5 +87,11 @@ public void IgnoreProperty(string propertyPath)
10087
{
10188
propertiesToIgnore.Add(propertyPath);
10289
}
90+
91+
/// <inheritdoc />
92+
public bool IsPropertyIgnored(string propertyPath)
93+
{
94+
return propertiesToIgnore.Contains(propertyPath);
95+
}
10396
}
104-
}
97+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using UnityEditor.AssetImporters;
2+
using UnityEngine;
3+
4+
[ScriptedImporter(1, "sample")]
5+
public class SampleScriptedImporter : ScriptedImporter
6+
{
7+
[SerializeField]
8+
private float sampleFloat;
9+
[SerializeField, InLineEditor]
10+
private SampleScriptableObject sampleScriptableObject;
11+
12+
public override void OnImportAsset(AssetImportContext ctx)
13+
{ }
14+
}

Assets/Examples/Editor/SampleScriptedImporter.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: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
using Toolbox.Editor.Editors;
2+
using UnityEditor;
3+
4+
[CustomEditor(typeof(SampleScriptedImporter))]
5+
public class SampleScriptedImporterEditor : ToolboxScriptedImporterEditor
6+
{ }

Assets/Examples/Editor/SampleScriptedImporterEditor.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/Examples/Samples.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.

Assets/Examples/Samples/Sample.sample

Whitespace-only changes.

0 commit comments

Comments
 (0)