Skip to content

Commit c106680

Browse files
authored
Merge pull request #19 from GilpStudio/master
Added compatibility whith Unity 2020.1 - Fix: #15
2 parents 097ae87 + 30ae37c commit c106680

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

Editor/ToolbarCallback.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,16 @@ public static class ToolbarCallback
1515
{
1616
static Type m_toolbarType = typeof(Editor).Assembly.GetType("UnityEditor.Toolbar");
1717
static Type m_guiViewType = typeof(Editor).Assembly.GetType("UnityEditor.GUIView");
18+
#if UNITY_2020_1_OR_NEWER
19+
static Type m_iWindowBackendType = typeof(Editor).Assembly.GetType("UnityEditor.IWindowBackend");
20+
static PropertyInfo m_windowBackend = m_guiViewType.GetProperty("windowBackend",
21+
BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
22+
static PropertyInfo m_viewVisualTree = m_iWindowBackendType.GetProperty("visualTree",
23+
BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
24+
#else
1825
static PropertyInfo m_viewVisualTree = m_guiViewType.GetProperty("visualTree",
1926
BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
27+
#endif
2028
static FieldInfo m_imguiContainerOnGui = typeof(IMGUIContainer).GetField("m_OnGUIHandler",
2129
BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
2230
static ScriptableObject m_currentToolbar;
@@ -42,8 +50,15 @@ static void OnUpdate()
4250
m_currentToolbar = toolbars.Length > 0 ? (ScriptableObject) toolbars[0] : null;
4351
if (m_currentToolbar != null)
4452
{
53+
#if UNITY_2020_1_OR_NEWER
54+
var windowBackend = m_windowBackend.GetValue(m_currentToolbar);
55+
56+
// Get it's visual tree
57+
var visualTree = (VisualElement) m_viewVisualTree.GetValue(windowBackend, null);
58+
#else
4559
// Get it's visual tree
4660
var visualTree = (VisualElement) m_viewVisualTree.GetValue(m_currentToolbar, null);
61+
#endif
4762

4863
// Get first child which 'happens' to be toolbar IMGUIContainer
4964
var container = (IMGUIContainer) visualTree[0];

Editor/ToolbarExtender.cs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ static ToolbarExtender()
2828
FieldInfo toolIcons = toolbarType.GetField(fieldName,
2929
BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
3030

31-
#if UNITY_2019_1_OR_NEWER
31+
#if UNITY_2019_3_OR_NEWER
32+
m_toolCount = toolIcons != null ? ((int) toolIcons.GetValue(null)) : 8;
33+
#elif UNITY_2019_1_OR_NEWER
3234
m_toolCount = toolIcons != null ? ((int) toolIcons.GetValue(null)) : 7;
3335
#elif UNITY_2018_1_OR_NEWER
3436
m_toolCount = toolIcons != null ? ((Array) toolIcons.GetValue(null)).Length : 6;
@@ -40,7 +42,11 @@ static ToolbarExtender()
4042
ToolbarCallback.OnToolbarGUI += OnGUI;
4143
}
4244

45+
#if UNITY_2019_3_OR_NEWER
46+
public const float space = 8;
47+
#else
4348
public const float space = 10;
49+
#endif
4450
public const float largeSpace = 20;
4551
public const float buttonWidth = 32;
4652
public const float dropdownWidth = 80;
@@ -68,7 +74,11 @@ static void OnGUI()
6874
Rect leftRect = new Rect(0, 0, screenWidth, Screen.height);
6975
leftRect.xMin += space; // Spacing left
7076
leftRect.xMin += buttonWidth * m_toolCount; // Tool buttons
77+
#if UNITY_2019_3_OR_NEWER
78+
leftRect.xMin += space; // Spacing between tools and pivot
79+
#else
7180
leftRect.xMin += largeSpace; // Spacing between tools and pivot
81+
#endif
7282
leftRect.xMin += 64 * 2; // Pivot buttons
7383
leftRect.xMax = playButtonsPosition;
7484

@@ -80,7 +90,11 @@ static void OnGUI()
8090
rightRect.xMax -= dropdownWidth; // Layout
8191
rightRect.xMax -= space; // Spacing between layout and layers
8292
rightRect.xMax -= dropdownWidth; // Layers
93+
#if UNITY_2019_3_OR_NEWER
94+
rightRect.xMax -= space; // Spacing between layers and account
95+
#else
8396
rightRect.xMax -= largeSpace; // Spacing between layers and account
97+
#endif
8498
rightRect.xMax -= dropdownWidth; // Account
8599
rightRect.xMax -= space; // Spacing between account and cloud
86100
rightRect.xMax -= buttonWidth; // Cloud
@@ -94,10 +108,17 @@ static void OnGUI()
94108
rightRect.xMax -= space;
95109

96110
// Add top and bottom margins
111+
#if UNITY_2019_3_OR_NEWER
112+
leftRect.y = 4;
113+
leftRect.height = 22;
114+
rightRect.y = 4;
115+
rightRect.height = 22;
116+
#else
97117
leftRect.y = 5;
98118
leftRect.height = 24;
99119
rightRect.y = 5;
100120
rightRect.height = 24;
121+
#endif
101122

102123
if (leftRect.width > 0)
103124
{

0 commit comments

Comments
 (0)