Skip to content
This repository was archived by the owner on May 9, 2025. It is now read-only.

Commit a31f77a

Browse files
Added support for runtimesets
1 parent 0563b33 commit a31f77a

File tree

3 files changed

+142
-2
lines changed

3 files changed

+142
-2
lines changed

Assets/SO Architecture/Editor/Inspectors/RuntimeSetEditor.cs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using UnityEditor;
33
using UnityEditorInternal;
44
using UnityEngine;
5+
using Type = System.Type;
56

67
[CustomEditor(typeof(BaseRuntimeSet), true)]
78
public class RuntimeSetEditor : Editor
@@ -11,6 +12,8 @@ public class RuntimeSetEditor : Editor
1112

1213
private ReorderableList _reorderableList;
1314

15+
private const bool DISABLE_ELEMENTS = false;
16+
1417
private void OnEnable()
1518
{
1619
SerializedProperty items = serializedObject.FindProperty("_items");
@@ -40,8 +43,23 @@ private void DrawElement(Rect rect, int index, bool isActive, bool isFocused)
4043

4144
SerializedProperty property = _reorderableList.serializedProperty.GetArrayElementAtIndex(index);
4245

43-
EditorGUI.BeginDisabledGroup(true);
44-
EditorGUI.ObjectField(rect, "Element " + index, property.objectReferenceValue, Target.Type, false);
46+
EditorGUI.BeginDisabledGroup(DISABLE_ELEMENTS);
47+
48+
EditorGUI.LabelField(rect, "Element " + index);
49+
50+
rect.width /= 2;
51+
rect.x += rect.width;
52+
53+
if (SOArchitecture_EditorUtility.HasPropertyDrawer(Target.Type))
54+
{
55+
EditorGUI.PropertyField(rect, property, new GUIContent(""));
56+
}
57+
else
58+
{
59+
EditorGUI.LabelField(rect, "No PropertyDrawer");
60+
}
61+
62+
//EditorGUI.ObjectField(rect, "Element " + index, property.objectReferenceValue, Target.Type, false);
4563
EditorGUI.EndDisabledGroup();
4664
}
4765
private void Remove(ReorderableList list)
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
using System.IO;
2+
using System.Reflection;
3+
using System.Collections;
4+
using System.Collections.Generic;
5+
using System.Text.RegularExpressions;
6+
using UnityEngine;
7+
using UnityEditor;
8+
using UnityEditor.Callbacks;
9+
using Type = System.Type;
10+
11+
public static class SOArchitecture_EditorUtility
12+
{
13+
static SOArchitecture_EditorUtility()
14+
{
15+
//We use this as a default since it'll be Assembly-CSharp-Editor
16+
_defaultTargetType = typeof(SOArchitecture_EditorUtility).Assembly;
17+
}
18+
19+
private static PropertyDrawerGraph _propertyDrawerGraph;
20+
private static Regex _removeWhiteSpace = new Regex(@"\s+");
21+
private static Assembly _defaultTargetType;
22+
private static BindingFlags _fieldBindingsFlag = BindingFlags.Instance | BindingFlags.NonPublic;
23+
24+
public static bool HasPropertyDrawer(Type type)
25+
{
26+
return HasPropertyDrawer(type, _defaultTargetType);
27+
}
28+
public static bool HasPropertyDrawer(Type type, Assembly assembly)
29+
{
30+
if (HasBuiltinPropertyDrawer(type))
31+
return true;
32+
33+
if (_propertyDrawerGraph == null)
34+
_propertyDrawerGraph = new PropertyDrawerGraph();
35+
36+
_propertyDrawerGraph.CreateGraph(assembly);
37+
38+
return _propertyDrawerGraph.HasPropertyDrawer(type);
39+
}
40+
private static bool HasBuiltinPropertyDrawer(Type type)
41+
{
42+
if (type.IsPrimitive || type == typeof(string) || typeof(Object).IsAssignableFrom(type))
43+
return true;
44+
45+
return false;
46+
}
47+
[DidReloadScripts]
48+
private static void OnProjectReloaded()
49+
{
50+
_propertyDrawerGraph = null;
51+
}
52+
53+
/// <summary>
54+
/// Goes through the entirety of the project and collects data about custom property drawers
55+
/// </summary>
56+
private class PropertyDrawerGraph
57+
{
58+
private List<Type> _supportedTypes = new List<Type>();
59+
private List<Type> _supportedInheritedTypes = new List<Type>();
60+
private List<Assembly> _checkedAssemblies = new List<Assembly>();
61+
62+
public bool HasPropertyDrawer(Type type)
63+
{
64+
foreach (Type supportedType in _supportedTypes)
65+
{
66+
if (supportedType == type)
67+
return true;
68+
}
69+
70+
foreach (Type inheritedSupportedType in _supportedInheritedTypes)
71+
{
72+
if (type.IsSubclassOf(inheritedSupportedType))
73+
return true;
74+
}
75+
76+
return false;
77+
}
78+
public void CreateGraph(Assembly assembly)
79+
{
80+
if (_checkedAssemblies.Contains(assembly))
81+
return;
82+
83+
_checkedAssemblies.Add(assembly);
84+
85+
foreach (Type type in assembly.GetTypes())
86+
{
87+
object[] attributes = type.GetCustomAttributes(typeof(CustomPropertyDrawer), false);
88+
89+
foreach (object attribute in attributes)
90+
{
91+
if(attribute is CustomPropertyDrawer)
92+
{
93+
CustomPropertyDrawer drawerData = attribute as CustomPropertyDrawer;
94+
95+
bool useForChildren = (bool)typeof(CustomPropertyDrawer).GetField("m_UseForChildren", _fieldBindingsFlag).GetValue(drawerData);
96+
Type targetType = (Type)typeof(CustomPropertyDrawer).GetField("m_Type", _fieldBindingsFlag).GetValue(drawerData);
97+
98+
if (useForChildren)
99+
{
100+
_supportedInheritedTypes.Add(targetType);
101+
}
102+
else
103+
{
104+
_supportedTypes.Add(targetType);
105+
}
106+
}
107+
}
108+
}
109+
}
110+
}
111+
}

Assets/SO Architecture/Editor/SOArchitecture_EditorUtility.cs.meta

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

0 commit comments

Comments
 (0)