Skip to content

Commit 1361246

Browse files
committed
Experimental UiToolkit support
1 parent 6981492 commit 1361246

File tree

8 files changed

+206
-3
lines changed

8 files changed

+206
-3
lines changed

Editor.Extras/Drawers/CustomBuiltInDrawer.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using TriInspector;
22
using TriInspector.Drawers;
3+
using TriInspector.Editors;
34
using TriInspector.Elements;
45
using TriInspector.Utilities;
56
using TriInspectorUnityInternalBridge;
@@ -24,6 +25,15 @@ public override TriElement CreateElement(TriValue<object> propertyValue, TriElem
2425

2526
if (drawWithHandler)
2627
{
28+
var visualElement = handler.CreatePropertyGUI(serializedProperty);
29+
30+
if (visualElement != null &&
31+
TriEditor.UiElementsRoots.TryGetValue(property.PropertyTree, out var rootElement))
32+
{
33+
return new TriUiToolkitPropertyElement(property, serializedProperty,
34+
visualElement, rootElement);
35+
}
36+
2737
return new TriBuiltInPropertyElement(property, serializedProperty, handler);
2838
}
2939
}

Editor/Editors/TriEditor.cs

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,79 @@
1+
using System;
2+
using System.Collections.Generic;
13
using TriInspector.Utilities;
24
using UnityEditor;
35
using UnityEngine;
6+
using UnityEngine.UIElements;
47

58
namespace TriInspector.Editors
69
{
710
public abstract class TriEditor : Editor
811
{
12+
internal static readonly Dictionary<TriPropertyTree, VisualElement> UiElementsRoots
13+
= new Dictionary<TriPropertyTree, VisualElement>();
14+
915
private TriPropertyTreeForSerializedObject _inspector;
1016

1117
private void OnDisable()
1218
{
1319
OnDisable(this, ref _inspector);
1420
}
1521

22+
public override VisualElement CreateInspectorGUI()
23+
{
24+
return CreateInspector(root => OnInspectorGUI(this, ref _inspector, root));
25+
}
26+
1627
public override void OnInspectorGUI()
1728
{
1829
OnInspectorGUI(this, ref _inspector);
1930
}
2031

2132
public static void OnDisable(Editor editor, ref TriPropertyTreeForSerializedObject inspector)
2233
{
23-
inspector?.Dispose();
34+
if (inspector != null)
35+
{
36+
UiElementsRoots.Remove(inspector);
37+
38+
inspector.Dispose();
39+
}
40+
2441
inspector = null;
2542
}
2643

44+
public static VisualElement CreateInspector(Action<VisualElement> onGui)
45+
{
46+
var container = new VisualElement();
47+
var root = new VisualElement()
48+
{
49+
style =
50+
{
51+
position = Position.Absolute,
52+
},
53+
};
54+
55+
container.Add(new IMGUIContainer(() =>
56+
{
57+
const float labelExtraPadding = 2;
58+
const float labelWidthRatio = 0.45f;
59+
const float labelMinWidth = 120;
60+
61+
var space = container.resolvedStyle.left + container.resolvedStyle.right + labelExtraPadding;
62+
63+
EditorGUIUtility.hierarchyMode = false;
64+
EditorGUIUtility.labelWidth = Mathf.Max(labelMinWidth,
65+
container.resolvedStyle.width * labelWidthRatio - space);
66+
67+
onGui?.Invoke(root);
68+
}));
69+
70+
container.Add(root);
71+
72+
return container;
73+
}
74+
2775
public static void OnInspectorGUI(Editor editor,
28-
ref TriPropertyTreeForSerializedObject inspector)
76+
ref TriPropertyTreeForSerializedObject inspector, VisualElement visualRoot = null)
2977
{
3078
var serializedObject = editor.serializedObject;
3179

@@ -54,6 +102,11 @@ public static void OnInspectorGUI(Editor editor,
54102
inspector = new TriPropertyTreeForSerializedObject(serializedObject);
55103
}
56104

105+
if (visualRoot != null)
106+
{
107+
UiElementsRoots[inspector] = visualRoot;
108+
}
109+
57110
serializedObject.UpdateIfRequiredOrScript();
58111

59112
inspector.Update();

Editor/Editors/TriScriptedImporterEditor.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using TriInspectorUnityInternalBridge;
22
using UnityEditor;
33
using UnityEditor.AssetImporters;
4+
using UnityEngine.UIElements;
45

56
namespace TriInspector.Editors
67
{
@@ -17,9 +18,19 @@ public override void OnDisable()
1718
base.OnDisable();
1819
}
1920

21+
public override VisualElement CreateInspectorGUI()
22+
{
23+
return TriEditor.CreateInspector(root => OnInspectorGUI(root));
24+
}
25+
2026
public override void OnInspectorGUI()
2127
{
22-
TriEditor.OnInspectorGUI(this, ref _inspector);
28+
OnInspectorGUI(null);
29+
}
30+
31+
private void OnInspectorGUI(VisualElement root)
32+
{
33+
TriEditor.OnInspectorGUI(this, ref _inspector, root);
2334

2435
if (extraDataType != null)
2536
{
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
using TriInspectorUnityInternalBridge;
2+
using UnityEditor;
3+
using UnityEditor.UIElements;
4+
using UnityEngine;
5+
using UnityEngine.UIElements;
6+
7+
namespace TriInspector.Elements
8+
{
9+
internal class TriUiToolkitPropertyElement : TriElement
10+
{
11+
private readonly SerializedProperty _serializedProperty;
12+
13+
private readonly VisualElement _rootElement;
14+
private readonly VisualElement _selfElement;
15+
16+
private bool _heightDirty;
17+
18+
public TriUiToolkitPropertyElement(
19+
TriProperty property,
20+
SerializedProperty serializedProperty,
21+
VisualElement selfElement,
22+
VisualElement rootElement)
23+
{
24+
_serializedProperty = serializedProperty;
25+
_selfElement = selfElement;
26+
_rootElement = rootElement;
27+
28+
_selfElement.style.position = Position.Absolute;
29+
}
30+
31+
protected override void OnAttachToPanel()
32+
{
33+
base.OnAttachToPanel();
34+
35+
_rootElement.schedule.Execute(() =>
36+
{
37+
_rootElement.Add(_selfElement);
38+
_selfElement.Bind(_serializedProperty.serializedObject);
39+
});
40+
}
41+
42+
protected override void OnDetachFromPanel()
43+
{
44+
_rootElement.schedule.Execute(() =>
45+
{
46+
_selfElement.Unbind();
47+
_rootElement.Remove(_selfElement);
48+
});
49+
50+
base.OnDetachFromPanel();
51+
}
52+
53+
public override bool Update()
54+
{
55+
var dirty = base.Update();
56+
57+
if (_heightDirty)
58+
{
59+
_heightDirty = false;
60+
dirty = true;
61+
}
62+
63+
return dirty;
64+
}
65+
66+
public override float GetHeight(float width)
67+
{
68+
var height = _selfElement.resolvedStyle.height;
69+
70+
if (float.IsNaN(height))
71+
{
72+
_heightDirty = true;
73+
return 0f;
74+
}
75+
76+
return height;
77+
}
78+
79+
public override void OnGUI(Rect position)
80+
{
81+
if (Event.current.type == EventType.Repaint)
82+
{
83+
var pos = GUIClipProxy.UnClip(position.position);
84+
85+
_selfElement.style.width = position.width;
86+
_selfElement.style.left = pos.x;
87+
_selfElement.style.top = pos.y;
88+
}
89+
}
90+
}
91+
}

Editor/Elements/TriUiToolkitPropertyElemenet.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using System;
2+
using UnityEditor;
3+
using UnityEngine;
4+
5+
namespace TriInspectorUnityInternalBridge
6+
{
7+
internal static class GUIClipProxy
8+
{
9+
private static Func<Vector2, Vector2> _guiClipUnClipVector2;
10+
11+
[InitializeOnLoadMethod]
12+
private static void Setup()
13+
{
14+
var imGuiModuleAssembly = typeof(GUI).Assembly;
15+
var guiClipType = imGuiModuleAssembly.GetType("UnityEngine.GUIClip", throwOnError: true);
16+
17+
_guiClipUnClipVector2 = (Func<Vector2, Vector2>) Delegate.CreateDelegate(typeof(Func<Vector2, Vector2>),
18+
guiClipType.GetMethod("Unclip", new[] {typeof(Vector2)}));
19+
}
20+
21+
public static Vector2 UnClip(Vector2 pos)
22+
{
23+
return _guiClipUnClipVector2.Invoke(pos);
24+
}
25+
}
26+
}

Unity.InternalAPIEditorBridge.012/GUIClipProxy.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Unity.InternalAPIEditorBridge.012/ScriptAttributeUtilityProxy.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using UnityEditor;
22
using UnityEngine;
3+
using UnityEngine.UIElements;
34

45
namespace TriInspectorUnityInternalBridge
56
{
@@ -24,6 +25,11 @@ internal PropertyHandlerProxy(PropertyHandler handler)
2425
// ReSharper disable once InconsistentNaming
2526
public bool hasPropertyDrawer => _handler.hasPropertyDrawer;
2627

28+
public VisualElement CreatePropertyGUI(SerializedProperty property)
29+
{
30+
return _handler.propertyDrawer?.CreatePropertyGUI(property);
31+
}
32+
2733
public float GetHeight(SerializedProperty property, GUIContent label, bool includeChildren)
2834
{
2935
return _handler.GetHeight(property, label, includeChildren);

0 commit comments

Comments
 (0)