Skip to content

Commit a5b089d

Browse files
authored
Merge pull request #72 from brunomikoski/feature/indirect-reference-property-drawer-revamp
Feature/indirect reference property drawer revamp
2 parents aa42c48 + 16e8c47 commit a5b089d

14 files changed

Lines changed: 223 additions & 163 deletions

CHANGELOG.MD

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

77

8+
## [1.5.6]
9+
### Changed
10+
- Improved the Refreshing of the `CollectionRegistry` to try to refresh when items are deleted/created
11+
- Refactored the `IndirectReference` to not need a serialized version of the `CollectionItem` type, so avoid any serialization issues
12+
- Renamed some of the editor classes to match the standard
13+
814
## [1.5.5]
915
### Changed
1016
- In order to support multiple collections of the same type the check of the `Values` I had to tweak how the instance of every instance its assigned, changed for the `OnEnable` of the collection item
@@ -22,7 +28,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2228
- The settings are now on the Project Preferences where you can define the default folder for the Scriptable Objects and default namespace.
2329
- Removed the `ReadOnlyList` on the `Collection`, the casting was expensive and was an unnecessary safety measure
2430

25-
2631
## [1.5.3]
2732
### Changed
2833
- Disabled the reload of the collection after script reloading on batch mode.
@@ -240,6 +245,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
240245
### Unreleased
241246

242247

248+
[1.5.6]: https://github.com/badawe/ScriptableObjectCollection/releases/tag/v1.5.6
243249
[1.5.5]: https://github.com/badawe/ScriptableObjectCollection/releases/tag/v1.5.5
244250
[1.5.4]: https://github.com/badawe/ScriptableObjectCollection/releases/tag/v1.5.4
245251
[1.5.3]: https://github.com/badawe/ScriptableObjectCollection/releases/tag/v1.5.3

Scripts/Editor/Core/CollectionCustomEditor.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,16 @@ private void OnDisable()
5959
ObjectUtility.SetDirty(collection);
6060
}
6161

62+
private void OnDestroy()
63+
{
64+
if (Application.isPlaying)
65+
return;
66+
67+
#if UNITY_EDITOR
68+
CollectionsRegistry.Instance.DeleteCollection(collection);
69+
#endif
70+
}
71+
6272
private void ValidateGUIDS()
6373
{
6474
collection.ValidateGUID();

Scripts/Editor/Core/CollectionItemDropdown.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,16 +66,16 @@ protected override void ItemSelected(AdvancedDropdownItem item)
6666
{
6767
ScriptableObjectCollection collection = collections.First();
6868
ScriptableObjectCollectionItem collectionItem = collection.AddNew(itemType);
69-
callback?.Invoke(collectionItem);
69+
callback.Invoke(collectionItem);
7070
Selection.objects = new Object[] {collection};
7171
CollectionCustomEditor.SetLastAddedEnum(collectionItem);
7272
return;
7373
}
7474

7575
if (item is CollectionItemDropdownItem dropdownItem)
76-
callback?.Invoke(dropdownItem.CollectionItem);
76+
callback.Invoke(dropdownItem.CollectionItem);
7777
else
78-
callback?.Invoke(null);
78+
callback.Invoke(null);
7979
}
8080

8181
public void Show(Rect rect, Action<ScriptableObjectCollectionItem> onSelectedCallback)

Scripts/Editor/Core/CollectionItemIndirectReferenceDrawer.cs

Lines changed: 0 additions & 85 deletions
This file was deleted.
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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+

Scripts/Editor/Core/CollectionItemIndirectReferenceDrawer.cs.meta renamed to Scripts/Editor/Core/CollectionItemIndirectReferencePropertyDrawer.cs.meta

File renamed without changes.

0 commit comments

Comments
 (0)