wangguan
2020-12-25 b35c810d34558ac4bcdbe382fae6db4cb3e3b545
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
81
using System;
using System.Reflection;
using UnityEngine;
using UnityEditor;
using Object = UnityEngine.Object;
 
namespace Core.Utilities.Editor
{
    /// <summary>
    /// Property drawer for serializable interfaces
    /// </summary>
    [CustomPropertyDrawer(typeof(SerializableInterface), true)]
    public class SerializableInterfaceDrawer : PropertyDrawer
    {
        /// <summary>
        /// Cached interface type that we get generically
        /// </summary>
        Type m_CachedInterfaceType;
        
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            SerializedProperty gameObjectProperty = property.FindPropertyRelative("unityObjectReference");
 
            // Try and find the interface type we need to filter for
            // Use ISerializableInterface by default
            Type interfaceType = typeof(ISerializableInterface);
            
            Object containingObject = property.serializedObject.targetObject;
            Type containingObjectType = property.serializedObject.targetObject.GetType();
            FieldInfo field = GetNestedField(containingObjectType, property.propertyPath);
 
            if (field != null)
            {
                Type serializableInterfaceType = FindAncestorSerializableType(field.FieldType);
                interfaceType = serializableInterfaceType.GetGenericArguments()[0];
            }
            
            EditorGUI.BeginProperty(position, label, property);
            EditorGUI.ObjectField(position, gameObjectProperty, interfaceType, 
                                  new GUIContent(property.displayName));
            EditorGUI.EndProperty();
        }
 
        static FieldInfo GetNestedField(Type owningType, string fieldPath)
        {
            while (true)
            {
                int firstDotIndex = fieldPath.IndexOf(".", StringComparison.Ordinal);
                if (firstDotIndex > 0)
                {
                    // Get first type and recurse in
                    string parentFieldName = fieldPath.Substring(0, firstDotIndex);
                    FieldInfo parentField = owningType.GetField(parentFieldName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
 
                    if (parentField == null)
                    {
                        return null;
                    }
                    owningType = parentField.FieldType;
                    fieldPath = fieldPath.Substring(firstDotIndex + 1);
                }
                else
                {
                    return owningType.GetField(fieldPath, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
                }
            }
        }
 
        static Type FindAncestorSerializableType(Type childType)
        {
            while (childType != null &&
                   !childType.IsGenericType &&
                   childType != typeof(SerializableInterface<>))
            {
                childType = childType.BaseType;
            }
 
            return childType;
        }
    }
}