Skip to content

Commit ee7031c

Browse files
committed
Unity 6.3 support: improvements to the Scene serialization process; new Toolbar changes adjustements
1 parent e749e56 commit ee7031c

File tree

11 files changed

+190
-31
lines changed

11 files changed

+190
-31
lines changed

Assets/Editor Toolbox/Editor/Drawers/Material/MaterialCompactTextureDrawer.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,11 @@ protected override void OnGUISafe(Rect position, MaterialProperty prop, string l
2727

2828
protected override bool IsPropertyValid(MaterialProperty prop)
2929
{
30+
#if UNITY_6000_3_OR_NEWER
31+
return prop.propertyType == UnityEngine.Rendering.ShaderPropertyType.Texture;
32+
#else
3033
return prop.type == MaterialProperty.PropType.Texture;
34+
#endif
3135
}
3236
}
3337
}

Assets/Editor Toolbox/Editor/Drawers/Material/MaterialConditionalDrawer.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,17 @@ protected MaterialConditionalDrawer(string togglePropertyName)
1212
this.togglePropertyName = togglePropertyName;
1313
}
1414

15+
private bool IsValidToggleType(MaterialProperty toggleProp)
16+
{
17+
#if UNITY_6000_3_OR_NEWER
18+
return toggleProp.propertyType == UnityEngine.Rendering.ShaderPropertyType.Float ||
19+
toggleProp.propertyType == UnityEngine.Rendering.ShaderPropertyType.Range;
20+
#else
21+
return toggleProp.type == MaterialProperty.PropType.Float ||
22+
toggleProp.type == MaterialProperty.PropType.Range;
23+
#endif
24+
}
25+
1526
protected override float GetPropertyHeightSafe(MaterialProperty prop, string label, MaterialEditor editor)
1627
{
1728
if (!HasToggle(prop))
@@ -48,7 +59,7 @@ protected virtual bool HasToggle(MaterialProperty prop)
4859
{
4960
var targets = prop.targets;
5061
var toggle = MaterialEditor.GetMaterialProperty(targets, togglePropertyName);
51-
return toggle != null && toggle.type == MaterialProperty.PropType.Float || toggle.type == MaterialProperty.PropType.Range;
62+
return toggle != null && IsValidToggleType(toggle);
5263
}
5364

5465
protected virtual bool? GetValue(MaterialProperty prop)

Assets/Editor Toolbox/Editor/Drawers/Material/MaterialMinMaxSliderDrawer.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,11 @@ protected override void OnGUISafe(Rect position, MaterialProperty prop, string l
4444

4545
protected override bool IsPropertyValid(MaterialProperty prop)
4646
{
47+
#if UNITY_6000_3_OR_NEWER
48+
return prop.propertyType == UnityEngine.Rendering.ShaderPropertyType.Vector;
49+
#else
4750
return prop.type == MaterialProperty.PropType.Vector;
51+
#endif
4852
}
4953
}
5054
}

Assets/Editor Toolbox/Editor/Drawers/Material/MaterialVector2Drawer.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,11 @@ protected override void OnGUISafe(Rect position, MaterialProperty prop, string l
3030

3131
protected override bool IsPropertyValid(MaterialProperty prop)
3232
{
33+
#if UNITY_6000_3_OR_NEWER
34+
return prop.propertyType == UnityEngine.Rendering.ShaderPropertyType.Vector;
35+
#else
3336
return prop.type == MaterialProperty.PropType.Vector;
37+
#endif
3438
}
3539
}
3640
}

Assets/Editor Toolbox/Editor/Drawers/Material/MaterialVector3Drawer.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,11 @@ protected override void OnGUISafe(Rect position, MaterialProperty prop, string l
3030

3131
protected override bool IsPropertyValid(MaterialProperty prop)
3232
{
33+
#if UNITY_6000_3_OR_NEWER
34+
return prop.propertyType == UnityEngine.Rendering.ShaderPropertyType.Vector;
35+
#else
3336
return prop.type == MaterialProperty.PropType.Vector;
37+
#endif
3438
}
3539
}
3640
}

Assets/Editor Toolbox/Editor/ToolboxEditorHierarchy.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,12 @@ private static void OnItemCallback(int instanceId, Rect rect)
4646
}
4747

4848
//use Unity's internal method to determinate the proper GameObject instance
49+
#if UNITY_6000_3_OR_NEWER
50+
var gameObject = EditorUtility.EntityIdToObject(instanceId) as GameObject;
51+
#else
4952
var gameObject = EditorUtility.InstanceIDToObject(instanceId) as GameObject;
50-
if (gameObject)
53+
#endif
54+
if (gameObject != null)
5155
{
5256
var type = GetLabelType(gameObject, out var label);
5357
//draw label using one of the possible forms

Assets/Editor Toolbox/Editor/ToolboxEditorToolbar.cs

Lines changed: 98 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55
using System.Reflection;
66

77
using UnityEditor;
8-
using UnityEngine;
98
using Object = UnityEngine.Object;
109
using Unity.EditorCoroutines.Editor;
10+
using UnityEngine;
11+
1112
#if UNITY_2019_1_OR_NEWER
1213
using UnityEngine.UIElements;
1314
#else
@@ -30,7 +31,11 @@ static ToolboxEditorToolbar()
3031
}
3132

3233
private static readonly Type containterType = typeof(IMGUIContainer);
34+
#if UNITY_6000_3_OR_NEWER
35+
private static readonly Type toolbarType = typeof(UnityEditor.Editor).Assembly.GetType("UnityEditor.MainToolbarWindow");
36+
#else
3337
private static readonly Type toolbarType = typeof(UnityEditor.Editor).Assembly.GetType("UnityEditor.Toolbar");
38+
#endif
3439
private static readonly Type guiViewType = typeof(UnityEditor.Editor).Assembly.GetType("UnityEditor.GUIView");
3540
#if UNITY_2020_1_OR_NEWER
3641
private static readonly Type backendType = typeof(UnityEditor.Editor).Assembly.GetType("UnityEditor.IWindowBackend");
@@ -55,24 +60,61 @@ private static IEnumerator Initialize()
5560
{
5661
while (toolbar == null)
5762
{
58-
var toolbars = Resources.FindObjectsOfTypeAll(toolbarType);
59-
if (toolbars == null || toolbars.Length == 0)
63+
if (!TryGetToolbarInstance(out toolbar))
6064
{
6165
yield return null;
6266
continue;
6367
}
64-
else
65-
{
66-
toolbar = toolbars[0];
67-
}
6868
}
6969

70+
#if UNITY_6000_3_OR_NEWER
71+
VisualElement root = null;
72+
if (toolbar is EditorWindow editorWindow)
73+
{
74+
root = editorWindow.rootVisualElement;
75+
}
76+
77+
var builder = root.Query<VisualElement>(name: "DockArea");
78+
var states = builder.Build();
79+
80+
var toolbarLeftZone = states.AtIndex(0);
81+
var leftElement = new VisualElement();
82+
leftElement.name = "Editor Toolbox Left Area";
83+
leftElement.StretchToParentSize();
84+
leftElement.style.left = 10;
85+
leftElement.style.right = 10;
86+
leftElement.style.flexGrow = 1;
87+
leftElement.style.flexDirection = FlexDirection.Row;
88+
89+
var leftContainer = new IMGUIContainer();
90+
leftContainer.style.flexGrow = 1;
91+
leftContainer.onGUIHandler = OnGuiLeft;
92+
leftElement.Add(leftContainer);
93+
toolbarLeftZone.Add(leftElement);
94+
95+
var toolbarRightZone = states.AtIndex(1);
96+
var rightElement = new VisualElement();
97+
rightElement.name = "Editor Toolbox Right Area";
98+
rightElement.StretchToParentSize();
99+
rightElement.style.left = 10;
100+
rightElement.style.right = 10;
101+
rightElement.style.flexGrow = 1;
102+
rightElement.style.flexDirection = FlexDirection.Row;
103+
104+
var rightContainer = new IMGUIContainer();
105+
rightContainer.style.flexGrow = 1;
106+
rightContainer.onGUIHandler = OnGuiRight;
107+
108+
rightElement.Add(rightContainer);
109+
toolbarRightZone.Add(rightElement);
110+
111+
#else
70112
#if UNITY_2021_1_OR_NEWER
71113
var rootField = toolbar.GetType().GetField("m_Root", BindingFlags.NonPublic | BindingFlags.Instance);
72114
var root = rootField.GetValue(toolbar) as VisualElement;
73115

74116
var toolbarLeftZone = root.Q("ToolbarZoneLeftAlign");
75-
var element = new VisualElement()
117+
var leftElement = new VisualElement()
76118
{
77119
style =
78120
{
@@ -81,11 +123,11 @@ private static IEnumerator Initialize()
81123
}
82124
};
83125

84-
var container = new IMGUIContainer();
85-
container.style.flexGrow = 1;
86-
container.onGUIHandler += OnGuiLeft;
87-
element.Add(container);
88-
toolbarLeftZone.Add(element);
126+
var leftContainer = new IMGUIContainer();
127+
leftContainer.style.flexGrow = 1;
128+
leftContainer.onGUIHandler = OnGuiLeft;
129+
leftElement.Add(leftContainer);
130+
toolbarLeftZone.Add(leftElement);
89131

90132
var toolbarRightZone = root.Q("ToolbarZoneRightAlign");
91133
var rightElement = new VisualElement()
@@ -99,7 +141,7 @@ private static IEnumerator Initialize()
99141

100142
var rightContainer = new IMGUIContainer();
101143
rightContainer.style.flexGrow = 1;
102-
rightContainer.onGUIHandler += OnGuiRight;
144+
rightContainer.onGUIHandler = OnGuiRight;
103145
rightElement.Add(rightContainer);
104146
toolbarRightZone.Add(rightElement);
105147
#else
@@ -119,6 +161,27 @@ private static IEnumerator Initialize()
119161
handler -= OnGuiLeft;
120162
handler += OnGuiLeft;
121163
onGuiHandler.SetValue(container, handler);
164+
#endif
165+
#endif
166+
}
167+
168+
private static bool TryGetToolbarInstance(out Object toolbarInstance)
169+
{
170+
#if UNITY_6000_3_OR_NEWER
171+
toolbarInstance = EditorWindow.GetWindow(toolbarType);
172+
return toolbarInstance != null;
173+
#else
174+
var toolbars = Resources.FindObjectsOfTypeAll(toolbarType);
175+
if (toolbars == null || toolbars.Length == 0)
176+
{
177+
toolbarInstance = null;
178+
return false;
179+
}
180+
else
181+
{
182+
toolbarInstance = toolbars[0];
183+
return true;
184+
}
122185
#endif
123186
}
124187

@@ -130,9 +193,15 @@ private static void OnGuiLeft()
130193
}
131194

132195
#if UNITY_2021_1_OR_NEWER
133-
using (new GUILayout.HorizontalScope())
196+
using (new EditorGUILayout.VerticalScope())
134197
{
135-
OnToolbarGuiLeft();
198+
GUILayout.FlexibleSpace();
199+
using (new EditorGUILayout.HorizontalScope())
200+
{
201+
OnToolbarGuiLeft();
202+
}
203+
204+
GUILayout.FlexibleSpace();
136205
}
137206
#else
138207
var screenWidth = EditorGUIUtility.currentViewWidth;
@@ -167,9 +236,15 @@ private static void OnGuiRight()
167236
return;
168237
}
169238

170-
using (new EditorGUILayout.HorizontalScope())
239+
using (new EditorGUILayout.VerticalScope())
171240
{
172-
OnToolbarGuiRight();
241+
GUILayout.FlexibleSpace();
242+
using (new EditorGUILayout.HorizontalScope())
243+
{
244+
OnToolbarGuiRight();
245+
}
246+
247+
GUILayout.FlexibleSpace();
173248
}
174249
}
175250

@@ -180,7 +255,12 @@ public static void Repaint()
180255
return;
181256
}
182257

258+
#if UNITY_6000_3_OR_NEWER
259+
var toolbarWindow = EditorWindow.GetWindow(toolbarType);
260+
toolbarWindow.Repaint();
261+
#else
183262
repaintMethod?.Invoke(toolbar, null);
263+
#endif
184264
}
185265

186266
public static bool IsToolbarAllowed { get; set; } = true;

Assets/Editor Toolbox/Runtime/Serialization/SceneSerializationUtility.cs

Lines changed: 51 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,30 @@ private static void Initialize()
2727
isInitialized = true;
2828
}
2929

30+
private static bool TryCreateSceneData(SceneAsset sceneAsset, out SceneData sceneData)
31+
{
32+
var path = AssetDatabase.GetAssetPath(sceneAsset);
33+
if (string.IsNullOrEmpty(path))
34+
{
35+
sceneData = null;
36+
return false;
37+
}
38+
39+
sceneData = new SceneData()
40+
{
41+
BuildIndex = SceneUtility.GetBuildIndexByScenePath(path),
42+
SceneName = sceneAsset.name,
43+
ScenePath = path,
44+
};
45+
46+
return true;
47+
}
48+
49+
private static bool CanRefreshCache()
50+
{
51+
return !EditorApplication.isUpdating;
52+
}
53+
3054
internal static void ConfirmCache()
3155
{
3256
//NOTE: refresh data only if the cache is empty,
@@ -39,6 +63,11 @@ internal static void ConfirmCache()
3963

4064
internal static void RefreshCache()
4165
{
66+
if (!CanRefreshCache())
67+
{
68+
return;
69+
}
70+
4271
cachedScenes.Clear();
4372
foreach (var scene in EditorBuildSettings.scenes)
4473
{
@@ -59,27 +88,41 @@ internal static void RefreshCache()
5988
continue;
6089
}
6190

62-
cachedScenes.Add(sceneAsset, new SceneData()
91+
if (TryCreateSceneData(sceneAsset, out var sceneData))
6392
{
64-
BuildIndex = SceneUtility.GetBuildIndexByScenePath(path),
65-
SceneName = sceneAsset.name,
66-
ScenePath = path
67-
});
93+
cachedScenes.Add(sceneAsset, sceneData);
94+
}
6895
}
6996

7097
OnCacheRefreshed?.Invoke();
7198
}
7299

73100
internal static bool TryGetSceneData(SceneAsset sceneAsset, out SceneData data)
74101
{
75-
ConfirmCache();
76-
if (!sceneAsset || !cachedScenes.TryGetValue(sceneAsset, out data))
102+
if (sceneAsset == null)
77103
{
78104
data = null;
79105
return false;
80106
}
81107

82-
return true;
108+
if (CanRefreshCache())
109+
{
110+
ConfirmCache();
111+
if (cachedScenes.TryGetValue(sceneAsset, out data))
112+
{
113+
return true;
114+
}
115+
}
116+
else
117+
{
118+
if (TryCreateSceneData(sceneAsset, out data))
119+
{
120+
return true;
121+
}
122+
}
123+
124+
data = null;
125+
return false;
83126
}
84127
#endif
85128
/// <summary>

Assets/Editor Toolbox/Runtime/Serialization/SerializedScene.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System;
2-
32
using Toolbox.Serialization;
43

54
#if UNITY_EDITOR

0 commit comments

Comments
 (0)