-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathSerializedPropertyExtensions.cs
80 lines (70 loc) · 2.08 KB
/
SerializedPropertyExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#if UNITY_EDITOR
using System.Collections.Generic;
using System.Reflection;
using UnityEditor;
using UnityEngine;
namespace cratesmith.assetui
{
public static class SerializedPropertyExtensions
{
public static string GetSanitizedPropertyPath(this SerializedProperty @this)
{
return @this.propertyPath.Replace(".Array.data[", "[");
}
public static System.Type GetSerializedPropertyType(this SerializedProperty @this)
{
// follow reflection up to match path and return type of last node
// fix path for arrays
var path = GetSanitizedPropertyPath(@this);
var currentType = @this.serializedObject.targetObject.GetType();
string[] slices = path.Split('.', '[');
foreach (var slice in slices)
{
if (currentType == null)
{
Debug.LogErrorFormat("GetSerializedPropertyType Couldn't extract type from {0}:{1}",
@this.serializedObject.targetObject.name,
@this.propertyPath);
return null;
}
// array element: get array type if this is an array element
if (slice.EndsWith("]"))
{
if (currentType.IsArray)
{
currentType = currentType.GetElementType();
}
else if (currentType.IsGenericType && currentType.GetGenericTypeDefinition().IsAssignableFrom(typeof(List<>)))
{
currentType = currentType.GetGenericArguments()[0];
}
else
{
Debug.LogErrorFormat("GetSerializedPropertyType unkown array/container type for {0}:{1}",
@this.serializedObject.targetObject.name,
@this.propertyPath);
return null;
}
}
else // field: find field by same name as slice and match to type
{
var type = currentType;
while (type != null)
{
var fieldInfo = type.GetField(slice, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
if (fieldInfo == null)
{
type = type.BaseType;
continue;
}
currentType = fieldInfo.FieldType;
break;
}
// Assert.IsNotNull(type);
}
}
return currentType;
}
}
}
#endif