-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSubclassPickerDrawer.cs
139 lines (124 loc) · 4.14 KB
/
SubclassPickerDrawer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#if UNITY_EDITOR
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using UnityEditor;
using UnityEditor.Experimental.GraphView;
using UnityEditor.UIElements;
using UnityEngine.UIElements;
#endif
using UnityEngine;
#if UNITY_EDITOR
[AttributeUsage(AttributeTargets.All)]
#endif
/// <summary>
/// Generic Assign property to its child classes.
/// Must be used with [SerializeReference] attribute.
/// </summary>
public class IsSubClass : PropertyAttribute
{
public IsSubClass()
{
#if UNITY_EDITOR
Debug.LogWarning("IsSubClass attribute should be used together with [SerializeReference]");
#endif
}
}
#if UNITY_EDITOR
[CustomPropertyDrawer(typeof(IsSubClass))]
public class IsSubClassDrawer : PropertyDrawer
{
private static Dictionary<Type, List<Type>> typeCache = new();
private static List<Type> GetSubclasses(Type baseType)
{
if (!typeCache.TryGetValue(baseType, out var subclasses))
{
subclasses = Assembly.GetAssembly(baseType)
.GetTypes()
.Where(t => t.IsClass && !t.IsAbstract && baseType.IsAssignableFrom(t))
.ToList();
typeCache[baseType] = subclasses;
}
return subclasses;
}
public override VisualElement CreatePropertyGUI(SerializedProperty property)
{
var container = new VisualElement
{
style = { flexDirection = FlexDirection.Row }
};
var dropdownButton = new Button(() =>
{
Type fieldType = fieldInfo.FieldType;
if (fieldType == null)
return;
var subclasses = GetSubclasses(fieldType);
SearchWindow.Open(new SearchWindowContext(GUIUtility.GUIToScreenPoint(Event.current.mousePosition)),
new SubclassSearchProvider(subclasses, selectedType =>
{
property.serializedObject.Update();
property.managedReferenceValue = Activator.CreateInstance(selectedType);
property.serializedObject.ApplyModifiedProperties();
}));
})
{
text = "▼",
style =
{
width = 20,
height = 20,
unityTextAlign = TextAnchor.MiddleCenter,
marginLeft = 5 // Add some spacing between the label and the button
}
};
var propertyField = new PropertyField(property)
{
style = { flexGrow = 1, marginLeft = 10 }
};
container.Add(propertyField);
container.Add(dropdownButton);
return container;
}
private string GetTypeName(SerializedProperty property)
{
var typeName = property.managedReferenceValue?.GetType().Name ?? "None";
return $"{typeName}";
}
private class SubclassSearchProvider : ScriptableObject, ISearchWindowProvider
{
private readonly List<Type> subclasses;
private readonly Action<Type> onTypeSelected;
public SubclassSearchProvider(List<Type> subclasses, Action<Type> onTypeSelected)
{
this.subclasses = subclasses;
this.onTypeSelected = onTypeSelected;
}
public List<SearchTreeEntry> CreateSearchTree(SearchWindowContext context)
{
var tree = new List<SearchTreeEntry>
{
new SearchTreeGroupEntry(new GUIContent("Select Type"), 0)
};
foreach (var subclass in subclasses)
{
tree.Add(new SearchTreeEntry(new GUIContent(subclass.Name))
{
level = 1,
userData = subclass
});
}
return tree;
}
public bool OnSelectEntry(SearchTreeEntry entry, SearchWindowContext context)
{
if (entry.userData is Type selectedType)
{
onTypeSelected(selectedType);
return true;
}
return false;
}
}
}
#endif