11using System.Collections.Generic;
13using System.Text.RegularExpressions;
28#pragma warning disable 0414
30#pragma warning restore 0414
33 if (!type.IsInterface) {
34 throw new System.Exception(type.Name +
" is not an interface.");
40 public void ConstrainValue(SerializedProperty property) {
41 if (property.objectReferenceValue !=
null) {
43 UnityObject implementingObject = FindImplementer(property,
44 property.objectReferenceValue);
46 if (implementingObject ==
null) {
47 Debug.LogError(property.objectReferenceValue.GetType().Name +
" does not implement " + type.Name);
50 property.objectReferenceValue = implementingObject;
66 bool isTypeDefinitelyIncompatible;
68 isTypeDefinitelyIncompatible = !this.type.IsAssignableFrom(fromObj.GetType());
73 var objIsGameObject = fromObj.GetType() == typeof(GameObject);
74 if (objIsGameObject) {
75 isTypeDefinitelyIncompatible =
false;
81 var objIsComponent = typeof(
Component).IsAssignableFrom(fromObj.GetType());
83 isTypeDefinitelyIncompatible =
false;
89 var fieldIsScriptableObject
90 =
fieldInfo.FieldType.IsAssignableFrom(typeof(ScriptableObject));
91 if (fieldIsScriptableObject && (objIsComponent || objIsGameObject)) {
92 isTypeDefinitelyIncompatible =
true;
95 var fieldTakesComponent
97 if (fieldTakesComponent && (!objIsComponent && !objIsGameObject)) {
98 isTypeDefinitelyIncompatible =
true;
101 if (isTypeDefinitelyIncompatible) {
105 if (fromObj.GetType().ImplementsInterface(type)) {
112 if (fromObj is GameObject) {
113 fromObj = (fromObj as GameObject).transform;
120 implementingObject = (fromObj as
Component)
121 .GetComponents<Component>()
123 .Where(c => c.GetType().ImplementsInterface(type))
128 implementingObject =
null;
131 return implementingObject;
135 public void DrawProperty(Rect rect, SerializedProperty property, GUIContent label) {
136 if (property.objectReferenceValue !=
null) {
137 EditorGUI.ObjectField(rect, property, type, label);
140 EditorGUI.ObjectField(rect, label,
null, type,
false);
144 public Rect GetDropArea(Rect rect, SerializedProperty property) {
148 public bool IsDropValid(
UnityObject[] draggedObjects, SerializedProperty property) {
149 return draggedObjects.Query().Any(o => FindImplementer(property, o) !=
null);
152 public void ProcessDroppedObjects(
UnityObject[] droppedObjects,
153 SerializedProperty property) {
155 var implementer = droppedObjects.Query()
156 .FirstOrDefault(o => FindImplementer(property, o));
158 if (implementer ==
null) {
159 Debug.LogError(property.objectReferenceValue.GetType().Name
160 +
" does not implement " + type.Name);
163 property.objectReferenceValue = implementer;
167 public override IEnumerable<SerializedPropertyType> SupportedTypes {
169 yield
return SerializedPropertyType.ObjectReference;
UnityEngine.Object UnityObject
UnityEngine.Component Component
ImplementsInterfaceAttribute(Type type)
UnityEngine.Object UnityObject