Skip to content

Commit 45ebbf4

Browse files
committed
Added better support for different types in RIST Editor
1 parent e78a21d commit 45ebbf4

File tree

3 files changed

+118
-32
lines changed

3 files changed

+118
-32
lines changed

Editor/Utilities/Extensions.cs

Lines changed: 112 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
using System.Reflection;
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using System.Reflection;
4+
using System.Linq;
25
using UnityEditor;
36

47
namespace Lachee.Utilities.Editor
@@ -23,17 +26,71 @@ public static System.Type GetSerializedType(this SerializedProperty property)
2326
/// <returns></returns>
2427
public static FieldInfo GetSerializedFieldInfo(this SerializedProperty property)
2528
{
26-
BindingFlags flag = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.GetField;
2729
System.Type parentType = property.serializedObject.targetObject.GetType();
28-
return parentType.GetFieldInfoFromPath(property.propertyPath, flag);
30+
return parentType.GetFieldInfoFromPath(property.propertyPath);
2931
}
3032

3133
/// <summary>
32-
/// Gets the field info from the given property path
34+
/// Gets the underlying value this property represents
35+
/// </summary>
36+
/// <param name="property"></param>
37+
/// <returns></returns>
38+
public static object GetSerializedValue(this SerializedProperty property)
39+
{
40+
#if !DISABLE_FAST_SERIALIZED_VALUE_LOOKUP
41+
switch (property.propertyType)
42+
{
43+
default: // If we cant find anything, we should just use the raw .ToString of the value
44+
case SerializedPropertyType.Enum: // Its easier to just lookup the enum properties than recreating it
45+
break;
46+
47+
// Manually get a bunch because its more efficient than looking up serialized values
48+
case SerializedPropertyType.ObjectReference:
49+
return property.objectReferenceValue ? property.objectReferenceValue.name : "[ NULL ]";
50+
case SerializedPropertyType.Boolean:
51+
return property.boolValue;
52+
case SerializedPropertyType.Integer:
53+
return property.intValue;
54+
case SerializedPropertyType.Float:
55+
return property.floatValue;
56+
case SerializedPropertyType.String:
57+
return property.stringValue;
58+
case SerializedPropertyType.Color:
59+
return property.colorValue;
60+
case SerializedPropertyType.Vector2:
61+
return property.vector2Value;
62+
case SerializedPropertyType.Vector3:
63+
return property.vector3Value;
64+
case SerializedPropertyType.Vector4:
65+
return property.vector4Value;
66+
case SerializedPropertyType.Vector2Int:
67+
return property.vector2IntValue;
68+
case SerializedPropertyType.Vector3Int:
69+
return property.vector3IntValue;
70+
case SerializedPropertyType.Quaternion:
71+
return property.quaternionValue;
72+
case SerializedPropertyType.Bounds:
73+
return property.boundsValue;
74+
case SerializedPropertyType.BoundsInt:
75+
return property.boundsIntValue;
76+
case SerializedPropertyType.Rect:
77+
return property.rectValue;
78+
case SerializedPropertyType.RectInt:
79+
return property.rectIntValue;
80+
}
81+
#endif
82+
// Lookup the property path and pull teh value directly
83+
System.Type parentType = property.serializedObject.targetObject.GetType();
84+
return parentType.GetValueFromPath(property.serializedObject.targetObject, property.propertyPath);
85+
}
86+
87+
/// <summary>
88+
/// Finds the field info for the given type at the given path.
3389
/// </summary>
3490
/// <param name="type"></param>
3591
/// <param name="path"></param>
3692
/// <param name="flag"></param>
93+
/// <remarks>Does not work with arrays yet as they would return a PropertyInfo instead</remarks>
3794
/// <returns></returns>
3895
public static FieldInfo GetFieldInfoFromPath(this System.Type type, string path, BindingFlags flag = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.GetField)
3996
{
@@ -56,58 +113,85 @@ public static FieldInfo GetFieldInfoFromPath(this System.Type type, string path,
56113
}
57114

58115
/// <summary>
59-
/// Gets the raw value of the property
116+
/// Gets the field values from the given path
60117
/// </summary>
61-
/// <param name="property"></param>
118+
/// <param name="type">The type of the root object</param>
119+
/// <param name="context">The root object to get the value from</param>
120+
/// <param name="path">The SerializedProperty formatted path</param>
121+
/// <param name="flag">The flag used to search fields.</param>
62122
/// <returns></returns>
63-
#if CSHARP_7_3_OR_NEWER && ENABLE_DYNAMIC
64-
public static dynamic GetSerializedValue(this SerializedProperty property) {
65-
dynamic result;
66-
#else
67-
public static object GetSerializedValue(this SerializedProperty property) {
68-
object result;
69-
#endif
70-
BindingFlags flag = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.GetField;
71-
result = property.serializedObject.targetObject;
72-
System.Type parentType = property.serializedObject.targetObject.GetType();
123+
public static object GetValueFromPath(this System.Type type, object context, string path, BindingFlags flag = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.GetField)
124+
{
125+
object result = context;
126+
System.Type resultType = type;
73127

74-
// See if we can return the object directly
75-
string path = property.propertyPath;
76-
FieldInfo fi = null;// = parentType.GetField(path, flag);
77-
78128
// We need to delve deeper until we hit the final result.
79-
string[] perDot = path.Split('.');
80-
foreach (string fieldName in perDot)
129+
string[] segments = path.Split('.');
130+
for (int i = 0; i < segments.Length; i++)
81131
{
82-
fi = parentType.GetField(fieldName, flag);
83-
if (fi != null)
132+
// If the field name is an array we need to break apart the next segment to extract its index.
133+
// Once we have the index we can then use the `this` property arrays have to get the appropriate item and
134+
// continue our search through the list of paths.
135+
string fieldName = segments[i];
136+
if (fieldName == "Array")
84137
{
85-
parentType = fi.FieldType;
86-
result = fi.GetValue(result);
138+
// parse the index
139+
string arrIndexPath = segments[++i];
140+
string arrIndexStr = arrIndexPath.Substring(5, arrIndexPath.Length - 1 - 5);
141+
int arrIndex = int.Parse(arrIndexStr);
142+
143+
// get the property
144+
var thisProperty = resultType.GetProperty("Item", new System.Type[] { arrIndex.GetType() });
145+
var thisGetter = thisProperty.GetMethod;
146+
147+
// Update the current state
148+
result = thisGetter.Invoke(result, new object[] { arrIndex });
149+
resultType = result.GetType();
87150
}
88151
else
89152
{
90-
return null;
153+
var fi = resultType.GetField(fieldName, flag);
154+
if (fi == null) return null;
155+
156+
resultType = fi.FieldType;
157+
result = fi.GetValue(result);
91158
}
92159
}
93160

94161
return result;
95162
}
96163

164+
97165
/// <summary>
98166
/// Determines the best name for the given property
99167
/// </summary>
100168
/// <param name="property"></param>
101169
/// <returns></returns>
102170
public static string GetReadableName(this SerializedProperty property)
103171
{
104-
switch(property.propertyType)
172+
switch (property.propertyType)
105173
{
106174
default:
107175
return property.displayName;
108176
case SerializedPropertyType.ObjectReference:
109177
return property.objectReferenceValue ? property.objectReferenceValue.name : "[ NULL ]";
110178
}
111179
}
180+
181+
/// <summary>
182+
/// Gets the value of the property as a string
183+
/// </summary>
184+
/// <param name="property"></param>
185+
/// <returns></returns>
186+
public static string GetValueName(this SerializedProperty property)
187+
{
188+
object value = property.GetSerializedValue();
189+
if (value == null) return "[ NULL ]";
190+
191+
if (value is float vFloat)
192+
return vFloat.ToString("n3");
193+
194+
return value.ToString();
195+
}
112196
}
113197
}

Editor/Utilities/RandomListDrawer.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,10 @@ private static ReorderableList CreateReoderableList(SerializedProperty property)
8383
{
8484
Event evt = Event.current;
8585
if (evt.type != EventType.Repaint) return;
86-
8786
var previous = GUI.backgroundColor;
88-
GUI.backgroundColor = RandomListStyle.Colors[index % RandomListStyle.Colors.Length];
87+
88+
if (index >= 0)
89+
GUI.backgroundColor = RandomListStyle.Colors[index % RandomListStyle.Colors.Length];
8990

9091
if (!isActive)
9192
GUI.backgroundColor *= 0.6f;
@@ -98,6 +99,7 @@ private static ReorderableList CreateReoderableList(SerializedProperty property)
9899
{
99100
itemListProperty.arraySize++;
100101
weightListProperty.arraySize++;
102+
weightListProperty.GetArrayElementAtIndex(weightListProperty.arraySize-1).floatValue = 1f;
101103
};
102104

103105
list.onRemoveCallback += (_) =>
@@ -338,7 +340,7 @@ class ItemWeightSerializedPair
338340
public SerializedProperty itemProperty;
339341
public SerializedProperty weightProperty;
340342

341-
public string name => itemProperty.GetReadableName();
343+
public string name => itemProperty.GetValueName();
342344
}
343345

344346
class WeightSlider : ItemWeightSerializedPair

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "com.lachee.utilities",
3-
"version": "1.3.0",
3+
"version": "1.3.1",
44
"displayName": "Lachee's Utilities",
55
"description": "Bunch of utility functionality",
66
"unity": "2019.1",

0 commit comments

Comments
 (0)