-
Notifications
You must be signed in to change notification settings - Fork 14
/
ToolbarManager.cs
180 lines (165 loc) · 5.14 KB
/
ToolbarManager.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
using System;
using System.Collections.Generic;
using System.Reflection;
using UnityEditor;
using UnityEngine;
#if UNITY_2019_1_OR_NEWER
using UnityEngine.UIElements;
#else
using UnityEngine.Experimental.UIElements;
#endif
[InitializeOnLoad]
public static class ToolbarManager
{
public static readonly List<Action> Actions = new List<Action>();
private static GUIStyle _GUIStyle = null;
static ToolbarManager()
{
ToolbarCallback.OnToolbarGUI = OnGUI;
ToolbarCallback.OnToolbarGUIButton = GUIButton;
}
#if UNITY_2019_3_OR_NEWER
public const float Space = 8;
#else
public const float Space = 10;
#endif
public const float LargeSpace = 20;
public const float ButtonWidth = 32;
public const float DropdownWidth = 80;
#if UNITY_2019_1_OR_NEWER
public const float PlayPauseStopWidth = 140;
#else
public const float PlayPauseStopWidth = 100;
#endif
static void OnGUI()
{
if (_GUIStyle == null) _GUIStyle = new GUIStyle("CommandLeft");
float screenWidth = EditorGUIUtility.currentViewWidth;
float playButtonsPosition = Mathf.RoundToInt ((screenWidth - PlayPauseStopWidth) / 2);
Rect rect = new Rect(0, 0, screenWidth, Screen.height);
rect.xMin = playButtonsPosition;
rect.xMin += _GUIStyle.fixedWidth * 3;
rect.xMax = screenWidth;
rect.xMax -= Space;
rect.xMax -= DropdownWidth;
rect.xMax -= Space;
rect.xMax -= DropdownWidth;
#if UNITY_2019_3_OR_NEWER
rect.xMax -= Space;
#else
rect.xMax -= LargeSpace;
#endif
rect.xMax -= DropdownWidth;
rect.xMax -= Space;
rect.xMax -= ButtonWidth;
rect.xMax -= Space;
rect.xMax -= 78;
rect.xMin += Space;
rect.xMax -= Space;
#if UNITY_2019_3_OR_NEWER
rect.y = 4;
rect.height = 22;
#else
rect.y = 5;
rect.height = 24;
#endif
if (rect.width > 0)
{
GUILayout.BeginArea(rect);
GUILayout.BeginHorizontal();
foreach (Action handler in Actions)
{
handler();
}
GUILayout.EndHorizontal();
GUILayout.EndArea();
}
}
public static void GUIButton()
{
GUILayout.BeginHorizontal();
foreach (Action handler in Actions)
{
handler();
}
GUILayout.EndHorizontal();
}
}
public static class ToolbarCallback
{
static BindingFlags _BindingFlags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance;
#if UNITY_2020_1_OR_NEWER
static PropertyInfo _WindowBackend = typeof(Editor).Assembly.GetType("UnityEditor.GUIView").GetProperty("windowBackend", _BindingFlags);
static PropertyInfo _VisualTree = typeof(Editor).Assembly.GetType("UnityEditor.IWindowBackend").GetProperty("visualTree", _BindingFlags);
#else
static PropertyInfo _VisualTree = typeof(Editor).Assembly.GetType("UnityEditor.GUIView").GetProperty("visualTree", _BindingFlags);
#endif
static FieldInfo _OnGUIHandler = typeof(IMGUIContainer).GetField("m_OnGUIHandler", _BindingFlags);
static ScriptableObject _ScriptableObject;
public static Action OnToolbarGUI;
public static Action OnToolbarGUIButton;
static ToolbarCallback()
{
EditorApplication.update -= OnUpdate;
EditorApplication.update += OnUpdate;
}
static void OnUpdate()
{
if (_ScriptableObject == null)
{
UnityEngine.Object[] toolbars = Resources.FindObjectsOfTypeAll(typeof(Editor).Assembly.GetType("UnityEditor.Toolbar"));
_ScriptableObject = toolbars.Length > 0 ? (ScriptableObject) toolbars[0] : null;
if (_ScriptableObject != null)
{
#if UNITY_2021_1_OR_NEWER
FieldInfo root = _ScriptableObject.GetType().GetField("m_Root", BindingFlags.NonPublic | BindingFlags.Instance);
VisualElement mRoot = root.GetValue(_ScriptableObject) as VisualElement;
VisualElement toolbarZone = mRoot.Q("ToolbarZoneRightAlign");
VisualElement parent = new VisualElement() {style = {flexGrow = 1, flexDirection = FlexDirection.Row,}};
IMGUIContainer container = new IMGUIContainer();
container.style.flexGrow = 1;
container.onGUIHandler += () => { OnToolbarGUIButton?.Invoke();};
parent.Add(container);
toolbarZone.Add(parent);
#else
#if UNITY_2020_1_OR_NEWER
System.Object windowBackend = _WindowBackend.GetValue(_ScriptableObject);
VisualElement visualTree = (VisualElement) _VisualTree.GetValue(windowBackend, null);
#else
VisualElement visualTree = (VisualElement) _VisualTree.GetValue(_ScriptableObject, null);
#endif
IMGUIContainer container = (IMGUIContainer) visualTree[0];
Action handler = (Action) _OnGUIHandler.GetValue(container);
handler -= OnGUI;
handler += OnGUI;
_OnGUIHandler.SetValue(container, handler);
#endif
}
}
}
static void OnGUI()
{
Action handler = OnToolbarGUI;
if (handler != null) handler();
}
}
[InitializeOnLoad]
public static class SimulateBuild
{
static bool Enabled = false;
static SimulateBuild()
{
ToolbarManager.Actions.Add(OnToolbarGUI);
}
static void OnToolbarGUI()
{
Texture texture = EditorGUIUtility.IconContent("Prefab Icon").image;
GUI.changed = false;
GUILayout.Toggle(Enabled, new GUIContent(null, texture, "Simulate Build"), "Command");
if (GUI.changed)
{
Enabled = !Enabled;
Debug.Log("Simulate build is: " + Enabled.ToString());
}
}
}