Skip to content

Commit e879517

Browse files
committed
Fix searching private fields while looking for the parent reference
1 parent 124f866 commit e879517

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

Assets/Editor Toolbox/Editor/Drawers/Helpers/Extraction/ValueExtractionHelper.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ public static class ValueExtractionHelper
1919
new MethodValueExtractor()
2020
};
2121

22-
2322
public static bool TryGetValue(string source, object declaringObject, out object value)
2423
{
2524
for (var i = 0; i < extractors.Count; i++)
@@ -73,7 +72,8 @@ public static bool TryGetValue(string source, SerializedProperty causer, out obj
7372
var parentObjects = new object[targetObjects.Length];
7473
for (var i = 0; i < targetObjects.Length; i++)
7574
{
76-
parentObjects[i] = causer.GetDeclaringObject(targetObjects[i]);
75+
var targetObject = targetObjects[i];
76+
parentObjects[i] = causer.GetDeclaringObject(targetObject);
7777
}
7878

7979
return TryGetValue(source, parentObjects, out value, out hasMixedValues, nextValuesComparer);

Assets/Editor Toolbox/Editor/Utilities/PropertyUtility.cs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,11 @@ public static object GetDeclaringObject(this SerializedProperty property, Object
9595
{
9696
var treeField = members[i];
9797
reference = GetTreePathReference(treeField, reference);
98+
if (reference == null)
99+
{
100+
continue;
101+
}
102+
98103
if (ignoreArrays && IsSerializableArrayType(reference))
99104
{
100105
continue;
@@ -119,8 +124,22 @@ public static object GetTreePathReference(string treeField, object treeParent)
119124
ToolboxEditorLog.LogError("Cannot parse array element properly.");
120125
}
121126

127+
const BindingFlags flags = BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public;
128+
122129
var fieldType = treeParent.GetType();
123-
var fieldInfo = fieldType.GetField(treeField, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
130+
FieldInfo fieldInfo = null;
131+
//NOTE: make sure to check in the base classes since there can be a private field/property
132+
while (fieldType != null)
133+
{
134+
fieldInfo = fieldType.GetField(treeField, flags);
135+
if (fieldInfo != null)
136+
{
137+
break;
138+
}
139+
140+
fieldType = fieldType.BaseType;
141+
}
142+
124143
if (fieldInfo == null)
125144
{
126145
ToolboxEditorLog.LogError($"Cannot find field: '{treeField}'.");
@@ -421,7 +440,6 @@ public static void OverrideLabelByValue(GUIContent label, SerializedProperty pro
421440
}
422441
}
423442

424-
425443
internal static class Defaults
426444
{
427445
internal static readonly string scriptPropertyName = "m_Script";
@@ -472,7 +490,6 @@ public static SerializedProperty GetSize(this SerializedProperty array)
472490
return array.FindPropertyRelative("Array.size");
473491
}
474492

475-
476493
public static T GetAttribute<T>(SerializedProperty property) where T : Attribute
477494
{
478495
return GetAttribute<T>(property, GetFieldInfo(property, out _));
@@ -493,7 +510,6 @@ public static T[] GetAttributes<T>(SerializedProperty property, FieldInfo fieldI
493510
return (T[])fieldInfo.GetCustomAttributes(typeof(T), true);
494511
}
495512

496-
497513
internal static void EnsureReflectionSafeness(SerializedProperty property)
498514
{
499515
if (property.serializedObject.hasModifiedProperties)

0 commit comments

Comments
 (0)