|
| 1 | +using System; |
| 2 | +using System.Linq; |
| 3 | +using UnityEditor; |
| 4 | +using UnityEngine; |
| 5 | + |
| 6 | +namespace BrunoMikoski.ScriptableObjectCollections |
| 7 | +{ |
| 8 | + [CustomPropertyDrawer(typeof(CollectionItemIndirectReference), true)] |
| 9 | + public sealed class CollectionItemIndirectReferencePropertyDrawer : PropertyDrawer |
| 10 | + { |
| 11 | + private const string COLLECTION_ITEM_GUID_PROPERTY_PATH = "collectionItemGUID"; |
| 12 | + private const string COLLECTION_GUID_PROPERTY_PATh = "collectionGUID"; |
| 13 | + |
| 14 | + private Type collectionItemType; |
| 15 | + private CollectionItemItemPropertyDrawer collectionItemPropertyDrawer; |
| 16 | + |
| 17 | + public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) |
| 18 | + { |
| 19 | + if (collectionItemType == null) |
| 20 | + { |
| 21 | + Type arrayOrListType = fieldInfo.FieldType.GetArrayOrListType(); |
| 22 | + Type properFieldType = arrayOrListType != null ? arrayOrListType : fieldInfo.FieldType; |
| 23 | + collectionItemType = GetGenericItemType(properFieldType).GetGenericArguments().First(); |
| 24 | + } |
| 25 | + |
| 26 | + if (collectionItemPropertyDrawer == null) |
| 27 | + { |
| 28 | + collectionItemPropertyDrawer = new CollectionItemItemPropertyDrawer(); |
| 29 | + collectionItemPropertyDrawer.Initialize(collectionItemType, null); |
| 30 | + } |
| 31 | + |
| 32 | + SerializedProperty collectionItemGUIDSerializedProperty = property.FindPropertyRelative(COLLECTION_ITEM_GUID_PROPERTY_PATH); |
| 33 | + SerializedProperty collectionGUIDSerializedProperty = property.FindPropertyRelative(COLLECTION_GUID_PROPERTY_PATh); |
| 34 | + |
| 35 | + ScriptableObjectCollectionItem collectionItem = null; |
| 36 | + |
| 37 | + if (!string.IsNullOrEmpty(collectionItemGUIDSerializedProperty.stringValue) |
| 38 | + && !string.IsNullOrEmpty(collectionGUIDSerializedProperty.stringValue)) |
| 39 | + { |
| 40 | + if (CollectionsRegistry.Instance.TryGetCollectionByGUID(collectionGUIDSerializedProperty.stringValue, |
| 41 | + out ScriptableObjectCollection collection)) |
| 42 | + { |
| 43 | + if (collection.TryGetItemByGUID(collectionItemGUIDSerializedProperty.stringValue, |
| 44 | + out ScriptableObjectCollectionItem resultCollection)) |
| 45 | + { |
| 46 | + collectionItem = resultCollection; |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + int indexOfArrayPart = property.propertyPath.IndexOf('['); |
| 52 | + |
| 53 | + if (indexOfArrayPart > -1) |
| 54 | + { |
| 55 | + if (string.Equals(label.text, collectionItemGUIDSerializedProperty.stringValue, StringComparison.Ordinal)) |
| 56 | + { |
| 57 | + label.text = $"Element {property.propertyPath.Substring(indexOfArrayPart+1, 1)}"; |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + collectionItemPropertyDrawer.DrawCollectionItemDrawer( |
| 62 | + position, collectionItem, label, |
| 63 | + item => |
| 64 | + { |
| 65 | + string collectionItemGUID = string.Empty; |
| 66 | + string collectionGUID = string.Empty; |
| 67 | + if (item != null) |
| 68 | + { |
| 69 | + collectionItemGUID = item.GUID; |
| 70 | + collectionGUID = item.Collection.GUID; |
| 71 | + } |
| 72 | + |
| 73 | + collectionItemGUIDSerializedProperty.stringValue = collectionItemGUID; |
| 74 | + collectionGUIDSerializedProperty.stringValue = collectionGUID; |
| 75 | + collectionItem = item; |
| 76 | + property.serializedObject.ApplyModifiedProperties(); |
| 77 | + } |
| 78 | + ); |
| 79 | + } |
| 80 | + |
| 81 | + private Type GetGenericItemType(Type targetType) |
| 82 | + { |
| 83 | + Type baseType = targetType.BaseType; |
| 84 | + |
| 85 | + while (baseType != null) |
| 86 | + { |
| 87 | + if (baseType.IsGenericType && baseType.GetGenericTypeDefinition() == typeof(CollectionItemIndirectReference<>)) |
| 88 | + return baseType; |
| 89 | + baseType = baseType.BaseType; |
| 90 | + } |
| 91 | + return null; |
| 92 | + } |
| 93 | + } |
| 94 | +} |
| 95 | + |
0 commit comments