Skip to content
This repository has been archived by the owner on Apr 10, 2024. It is now read-only.

Commit

Permalink
Update SerializedPropertyExtensions.cs
Browse files Browse the repository at this point in the history
  • Loading branch information
uurha committed Oct 29, 2023
1 parent 25387fa commit 0ab3b2e
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,8 @@ public static string GetPropertyParentList(this SerializedProperty property)

public static string GetPropertyParentList(string propertyPath)
{

int length = propertyPath.LastIndexOf(".Array.data[", StringComparison.Ordinal);
return length < 0 ? (string) null : propertyPath.Substring(0, length);
return length < 0 ? (string)null : propertyPath.Substring(0, length);
}

public static string GetArrayNameFromPath(this SerializedProperty property)
Expand Down Expand Up @@ -242,12 +241,12 @@ private static bool IsCollectionType(this Type targetType)

return false;
}

public static List<object> GetPropertyContainers(this SerializedProperty property)
{
string propertyPath = property.propertyPath;
object container = property.serializedObject.targetObject;

int i = 0;
PropertyPathComponent deferredToken;
var list = new List<object>();
Expand Down Expand Up @@ -278,7 +277,7 @@ private static object GetPropertyContainer(SerializedProperty property, out Prop
container = GetPathComponentValue(container, deferredToken);
deferredToken = token;
}

return container;
}

Expand Down Expand Up @@ -364,18 +363,37 @@ private static object GetMemberValue(object container, string name)
if (container == null)
return null;
var type = container.GetType();
var members = type.GetMember(name, BetterEditorDefines.FieldsFlags);
for (int i = 0; i < members.Length; ++i)
var members = TraverseBaseClasses(type, name);
for (int i = 0; i < members.Count; ++i)
{
if (members[i] is FieldInfo field)
return field.GetValue(container);
else if (members[i] is PropertyInfo property)

if (members[i] is PropertyInfo property)
return property.GetValue(container);
}

return null;
}

private static List<MemberInfo> TraverseBaseClasses(Type currentType, string name)
{
var memberInfos = new List<MemberInfo>();

var currentTypeFields = currentType.GetMember(name, BetterEditorDefines.FieldsFlags);
memberInfos.AddRange(currentTypeFields);

var baseType = currentType.BaseType;

if (baseType == null) return memberInfos;
var baseTypeFields = baseType.GetMember(name, BetterEditorDefines.FieldsFlags);
memberInfos.AddRange(baseTypeFields);

memberInfos.AddRange(TraverseBaseClasses(baseType, name)); // Recursively traverse the base classes

return memberInfos;
}

private static void SetMemberValue(object container, string name, object value)
{
var type = container.GetType();
Expand All @@ -384,14 +402,15 @@ private static void SetMemberValue(object container, string name, object value)
{
if (members[i] is FieldInfo field)
{
if(!type.IsValueType && !type.IsEnum)
if (!type.IsValueType && !type.IsEnum)
{
field.SetValue(container, value);
}
else
{
field.SetValueDirect(__makeref(container), value);
}

return;
}
else if (members[i] is PropertyInfo property)
Expand Down
2 changes: 1 addition & 1 deletion Assets/BetterEditorTools/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "com.uurha.bettereditortools",
"displayName": "Better Editor Tools",
"description": "Collection of useful tools for Unity Editor",
"version": "1.0.66",
"version": "1.0.67",
"unity": "2020.1",
"dependencies": {
"com.uurha.betterdatastructures": "0.1.1",
Expand Down

0 comments on commit 0ab3b2e

Please sign in to comment.