@@ -29,7 +29,6 @@ static ToolboxEditorToolbar()
29
29
EditorCoroutineUtility . StartCoroutineOwnerless ( Initialize ( ) ) ;
30
30
}
31
31
32
-
33
32
private static readonly Type containterType = typeof ( IMGUIContainer ) ;
34
33
private static readonly Type toolbarType = typeof ( UnityEditor . Editor ) . Assembly . GetType ( "UnityEditor.Toolbar" ) ;
35
34
private static readonly Type guiViewType = typeof ( UnityEditor . Editor ) . Assembly . GetType ( "UnityEditor.GUIView" ) ;
@@ -49,12 +48,10 @@ static ToolboxEditorToolbar()
49
48
50
49
private static Object toolbar ;
51
50
52
-
53
51
private static IEnumerator Initialize ( )
54
52
{
55
- while ( toolbar == null )
53
+ while ( ToolboxEditorToolbar . toolbar == null )
56
54
{
57
- //try to find aldready created Toolbar object
58
55
var toolbars = Resources . FindObjectsOfTypeAll ( toolbarType ) ;
59
56
if ( toolbars == null || toolbars . Length == 0 )
60
57
{
@@ -63,9 +60,31 @@ private static IEnumerator Initialize()
63
60
}
64
61
else
65
62
{
66
- toolbar = toolbars [ 0 ] ;
63
+ ToolboxEditorToolbar . toolbar = toolbars [ 0 ] ;
67
64
}
68
65
}
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
69
88
#if UNITY_2020_1_OR_NEWER
70
89
var backend = guiBackend . GetValue ( toolbar ) ;
71
90
var elements = visualTree . GetValue ( backend , null ) as VisualElement ;
@@ -78,11 +97,11 @@ private static IEnumerator Initialize()
78
97
#else
79
98
var container = elements [ 0 ] as IMGUIContainer ;
80
99
#endif
81
- //create additional gui handler for new elements
82
100
var handler = onGuiHandler . GetValue ( container ) as Action ;
83
101
handler -= OnGui ;
84
102
handler += OnGui ;
85
103
onGuiHandler . SetValue ( container , handler ) ;
104
+ #endif
86
105
}
87
106
88
107
private static void OnGui ( )
@@ -92,12 +111,17 @@ private static void OnGui()
92
111
return ;
93
112
}
94
113
114
+ #if UNITY_2021_1_OR_NEWER
115
+ using ( new GUILayout . HorizontalScope ( ) )
116
+ {
117
+ OnToolbarGui . Invoke ( ) ;
118
+ }
119
+ #else
95
120
var screenWidth = EditorGUIUtility . currentViewWidth ;
96
121
var toolbarRect = new Rect ( 0 , 0 , screenWidth , Style . rowHeight ) ;
97
122
//calculations known from UnityCsReference
98
123
toolbarRect . xMin += FromToolsOffset ;
99
124
toolbarRect . xMax = ( screenWidth - FromStripOffset ) / 2 ;
100
- //additional rect styling
101
125
toolbarRect . xMin += Style . spacing ;
102
126
toolbarRect . xMax -= Style . spacing ;
103
127
toolbarRect . yMin += Style . topPadding ;
@@ -108,14 +132,14 @@ private static void OnGui()
108
132
return ;
109
133
}
110
134
111
- //begin drawing in calculated area
112
135
using ( new GUILayout . AreaScope ( toolbarRect ) )
113
136
{
114
137
using ( new GUILayout . HorizontalScope ( ) )
115
138
{
116
139
OnToolbarGui ? . Invoke ( ) ;
117
140
}
118
141
}
142
+ #endif
119
143
}
120
144
121
145
@@ -124,10 +148,8 @@ private static void OnGui()
124
148
public static float FromToolsOffset { get ; set ; } = 400.0f ;
125
149
public static float FromStripOffset { get ; set ; } = 150.0f ;
126
150
127
-
128
151
public static event Action OnToolbarGui ;
129
152
130
-
131
153
private static class Style
132
154
{
133
155
internal static readonly float rowHeight = 30.0f ;
0 commit comments