Skip to content

Commit eff39a3

Browse files
committed
Fix ToolboxEditorToolbar behaviour in Unity 2021.1+
1 parent 8cdab67 commit eff39a3

File tree

1 file changed

+32
-10
lines changed

1 file changed

+32
-10
lines changed

Assets/Editor Toolbox/Editor/ToolboxEditorToolbar.cs

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ static ToolboxEditorToolbar()
2929
EditorCoroutineUtility.StartCoroutineOwnerless(Initialize());
3030
}
3131

32-
3332
private static readonly Type containterType = typeof(IMGUIContainer);
3433
private static readonly Type toolbarType = typeof(UnityEditor.Editor).Assembly.GetType("UnityEditor.Toolbar");
3534
private static readonly Type guiViewType = typeof(UnityEditor.Editor).Assembly.GetType("UnityEditor.GUIView");
@@ -49,12 +48,10 @@ static ToolboxEditorToolbar()
4948

5049
private static Object toolbar;
5150

52-
5351
private static IEnumerator Initialize()
5452
{
55-
while (toolbar == null)
53+
while (ToolboxEditorToolbar.toolbar == null)
5654
{
57-
//try to find aldready created Toolbar object
5855
var toolbars = Resources.FindObjectsOfTypeAll(toolbarType);
5956
if (toolbars == null || toolbars.Length == 0)
6057
{
@@ -63,9 +60,31 @@ private static IEnumerator Initialize()
6360
}
6461
else
6562
{
66-
toolbar = toolbars[0];
63+
ToolboxEditorToolbar.toolbar = toolbars[0];
6764
}
6865
}
66+
67+
#if UNITY_2021_1_OR_NEWER
68+
var rootField = ToolboxEditorToolbar.toolbar.GetType().GetField("m_Root", BindingFlags.NonPublic | BindingFlags.Instance);
69+
var root = rootField.GetValue(ToolboxEditorToolbar.toolbar) as VisualElement;
70+
var toolbar = root.Q("ToolbarZoneLeftAlign");
71+
72+
var element = new VisualElement()
73+
{
74+
style =
75+
{
76+
flexGrow = 1,
77+
flexDirection = FlexDirection.Row,
78+
}
79+
};
80+
81+
var container = new IMGUIContainer();
82+
container.style.flexGrow = 1;
83+
container.onGUIHandler += OnGui;
84+
85+
element.Add(container);
86+
toolbar.Add(element);
87+
#else
6988
#if UNITY_2020_1_OR_NEWER
7089
var backend = guiBackend.GetValue(toolbar);
7190
var elements = visualTree.GetValue(backend, null) as VisualElement;
@@ -78,11 +97,11 @@ private static IEnumerator Initialize()
7897
#else
7998
var container = elements[0] as IMGUIContainer;
8099
#endif
81-
//create additional gui handler for new elements
82100
var handler = onGuiHandler.GetValue(container) as Action;
83101
handler -= OnGui;
84102
handler += OnGui;
85103
onGuiHandler.SetValue(container, handler);
104+
#endif
86105
}
87106

88107
private static void OnGui()
@@ -92,12 +111,17 @@ private static void OnGui()
92111
return;
93112
}
94113

114+
#if UNITY_2021_1_OR_NEWER
115+
using (new GUILayout.HorizontalScope())
116+
{
117+
OnToolbarGui.Invoke();
118+
}
119+
#else
95120
var screenWidth = EditorGUIUtility.currentViewWidth;
96121
var toolbarRect = new Rect(0, 0, screenWidth, Style.rowHeight);
97122
//calculations known from UnityCsReference
98123
toolbarRect.xMin += FromToolsOffset;
99124
toolbarRect.xMax = (screenWidth - FromStripOffset) / 2;
100-
//additional rect styling
101125
toolbarRect.xMin += Style.spacing;
102126
toolbarRect.xMax -= Style.spacing;
103127
toolbarRect.yMin += Style.topPadding;
@@ -108,14 +132,14 @@ private static void OnGui()
108132
return;
109133
}
110134

111-
//begin drawing in calculated area
112135
using (new GUILayout.AreaScope(toolbarRect))
113136
{
114137
using (new GUILayout.HorizontalScope())
115138
{
116139
OnToolbarGui?.Invoke();
117140
}
118141
}
142+
#endif
119143
}
120144

121145

@@ -124,10 +148,8 @@ private static void OnGui()
124148
public static float FromToolsOffset { get; set; } = 400.0f;
125149
public static float FromStripOffset { get; set; } = 150.0f;
126150

127-
128151
public static event Action OnToolbarGui;
129152

130-
131153
private static class Style
132154
{
133155
internal static readonly float rowHeight = 30.0f;

0 commit comments

Comments
 (0)