Skip to content

Commit 2d61980

Browse files
committed
Toolbox equivalent of the ScriptableWizard class
1 parent 7180aa5 commit 2d61980

5 files changed

Lines changed: 178 additions & 0 deletions

File tree

Assets/Editor Toolbox/Editor/Windows.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: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
using UnityEditor;
2+
using UnityEngine;
3+
4+
namespace Toolbox.Editor.Wizards
5+
{
6+
using Editor = UnityEditor.Editor;
7+
8+
public class ToolboxWizard : EditorWindow
9+
{
10+
private Editor targetEditor;
11+
12+
private Vector2 scrollPosition;
13+
14+
private void OnDestroy()
15+
{
16+
DestroyImmediate(targetEditor);
17+
}
18+
19+
private void OnGUI()
20+
{
21+
using (var scrollView = new EditorGUILayout.ScrollViewScope(scrollPosition))
22+
{
23+
scrollPosition = scrollView.scrollPosition;
24+
EditorGUI.BeginChangeCheck();
25+
OnWizardGui();
26+
if (EditorGUI.EndChangeCheck())
27+
{
28+
OnWizardUpdate();
29+
}
30+
}
31+
32+
using (new EditorGUILayout.VerticalScope())
33+
{
34+
GUILayout.FlexibleSpace();
35+
using (new EditorGUILayout.HorizontalScope())
36+
{
37+
GUILayout.FlexibleSpace();
38+
HandleOtherButtons();
39+
GUI.enabled = IsValid;
40+
if (HandleCreateButton())
41+
{
42+
OnWizardCreate();
43+
Close();
44+
GUIUtility.ExitGUI();
45+
}
46+
47+
GUI.enabled = true;
48+
}
49+
50+
GUILayout.Space(5);
51+
}
52+
}
53+
54+
private void PrepareEditor()
55+
{
56+
if (targetEditor != null)
57+
{
58+
return;
59+
}
60+
61+
targetEditor = Editor.CreateEditor(this);
62+
targetEditor.hideFlags = HideFlags.HideAndDontSave;
63+
OnWizardUpdate();
64+
}
65+
66+
protected virtual void OnWizardCreate()
67+
{ }
68+
69+
protected virtual void OnWizardUpdate()
70+
{ }
71+
72+
protected virtual void OnWizardGui()
73+
{
74+
PrepareEditor();
75+
targetEditor.OnInspectorGUI();
76+
}
77+
78+
protected virtual bool HandleCreateButton()
79+
{
80+
return GUILayout.Button("Create", GUILayout.MinWidth(100));
81+
}
82+
83+
protected virtual void HandleOtherButtons()
84+
{ }
85+
86+
public static T DisplayWizard<T>(string title) where T : ToolboxWizard
87+
{
88+
return GetWindow<T>(true, title);
89+
}
90+
91+
protected bool IsValid { get; set; } = true;
92+
}
93+
}

Assets/Editor Toolbox/Editor/Windows/ToolboxWizard.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: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
using Toolbox.Editor.Wizards;
2+
3+
using UnityEditor;
4+
using UnityEngine;
5+
6+
public class SampleWizard : ToolboxWizard
7+
{
8+
[SerializeField, InLineEditor]
9+
private GameObject prefab;
10+
[SerializeField]
11+
private string targetName;
12+
13+
private bool hasInvalidName;
14+
15+
[MenuItem("GameObject/Sample Wizard")]
16+
public static void CreateWizard()
17+
{
18+
DisplayWizard<SampleWizard>("Create GameObject");
19+
}
20+
21+
protected override void OnWizardCreate()
22+
{
23+
GameObject go;
24+
if (prefab != null)
25+
{
26+
go = Instantiate(prefab);
27+
go.name = targetName;
28+
}
29+
else
30+
{
31+
go = new GameObject(targetName);
32+
}
33+
34+
Selection.activeObject = go;
35+
}
36+
37+
protected override void OnWizardUpdate()
38+
{
39+
hasInvalidName = string.IsNullOrEmpty(targetName);
40+
}
41+
42+
protected override void OnWizardGui()
43+
{
44+
base.OnWizardGui();
45+
if (hasInvalidName)
46+
{
47+
EditorGUILayout.HelpBox("Name is invalid", MessageType.Error, true);
48+
}
49+
}
50+
51+
protected override void HandleOtherButtons()
52+
{
53+
//you can draw more buttons here
54+
}
55+
}

Assets/Examples/Editor/SampleWizard.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)