Skip to content

Commit

Permalink
Fix searching private fields while looking for the parent reference
Browse files Browse the repository at this point in the history
  • Loading branch information
arimger committed Jul 28, 2024
1 parent 124f866 commit e879517
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ public static class ValueExtractionHelper
new MethodValueExtractor()
};


public static bool TryGetValue(string source, object declaringObject, out object value)
{
for (var i = 0; i < extractors.Count; i++)
Expand Down Expand Up @@ -73,7 +72,8 @@ public static bool TryGetValue(string source, SerializedProperty causer, out obj
var parentObjects = new object[targetObjects.Length];
for (var i = 0; i < targetObjects.Length; i++)
{
parentObjects[i] = causer.GetDeclaringObject(targetObjects[i]);
var targetObject = targetObjects[i];
parentObjects[i] = causer.GetDeclaringObject(targetObject);
}

return TryGetValue(source, parentObjects, out value, out hasMixedValues, nextValuesComparer);
Expand Down
24 changes: 20 additions & 4 deletions Assets/Editor Toolbox/Editor/Utilities/PropertyUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,11 @@ public static object GetDeclaringObject(this SerializedProperty property, Object
{
var treeField = members[i];
reference = GetTreePathReference(treeField, reference);
if (reference == null)
{
continue;
}

if (ignoreArrays && IsSerializableArrayType(reference))
{
continue;
Expand All @@ -119,8 +124,22 @@ public static object GetTreePathReference(string treeField, object treeParent)
ToolboxEditorLog.LogError("Cannot parse array element properly.");
}

const BindingFlags flags = BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public;

var fieldType = treeParent.GetType();
var fieldInfo = fieldType.GetField(treeField, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
FieldInfo fieldInfo = null;
//NOTE: make sure to check in the base classes since there can be a private field/property
while (fieldType != null)
{
fieldInfo = fieldType.GetField(treeField, flags);
if (fieldInfo != null)
{
break;
}

fieldType = fieldType.BaseType;
}

if (fieldInfo == null)
{
ToolboxEditorLog.LogError($"Cannot find field: '{treeField}'.");
Expand Down Expand Up @@ -421,7 +440,6 @@ public static void OverrideLabelByValue(GUIContent label, SerializedProperty pro
}
}


internal static class Defaults
{
internal static readonly string scriptPropertyName = "m_Script";
Expand Down Expand Up @@ -472,7 +490,6 @@ public static SerializedProperty GetSize(this SerializedProperty array)
return array.FindPropertyRelative("Array.size");
}


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


internal static void EnsureReflectionSafeness(SerializedProperty property)
{
if (property.serializedObject.hasModifiedProperties)
Expand Down

0 comments on commit e879517

Please sign in to comment.