Skip to content

Commit f2f273c

Browse files
committed
fix: indirect references + small tweaks
1 parent 7724ec3 commit f2f273c

4 files changed

Lines changed: 27 additions & 20 deletions

File tree

Scripts/Editor/CollectableIndirectReferenceDrawer.cs

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ public sealed class CollectableIndirectReferenceDrawer : PropertyDrawer
1515
private SerializedProperty collectableGUIDProperty;
1616
private SerializedProperty collectionGUIDProperty;
1717

18-
private CollectableScriptableObject collectableScriptableObject;
1918
private CollectableScriptableObject cachedReference;
2019

2120
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
@@ -24,24 +23,40 @@ public override void OnGUI(Rect position, SerializedProperty property, GUIConten
2423
collectableGUIDProperty = property.FindPropertyRelative(COLLECTABLE_GUID_PROPERTY_PATH);
2524
collectionGUIDProperty = property.FindPropertyRelative(COLLECTION_GUID_PROPERTY_PATh);
2625

27-
if (cachedReference == null
28-
&& !string.IsNullOrEmpty(collectableGUIDProperty.stringValue)
29-
&& !string.IsNullOrEmpty(collectionGUIDProperty.stringValue))
26+
if (objectAssetProperty.objectReferenceValue != null)
3027
{
31-
if (CollectionsRegistry.Instance.TryGetCollectionByGUID(collectionGUIDProperty.stringValue,
32-
out ScriptableObjectCollection collection))
28+
if (string.IsNullOrEmpty(collectableGUIDProperty.stringValue)
29+
|| string.IsNullOrEmpty(collectionGUIDProperty.stringValue))
3330
{
34-
if (collection.TryGetCollectableByGUID(collectableGUIDProperty.stringValue,
35-
out CollectableScriptableObject collectable))
31+
CollectableScriptableObject collectable = objectAssetProperty.objectReferenceValue as CollectableScriptableObject;
32+
collectableGUIDProperty.stringValue = collectable.GUID;
33+
collectionGUIDProperty.stringValue = collectable.Collection.GUID;
34+
objectAssetProperty.serializedObject.ApplyModifiedProperties();
35+
}
36+
}
37+
else
38+
{
39+
if (!string.IsNullOrEmpty(collectableGUIDProperty.stringValue)
40+
&& !string.IsNullOrEmpty(collectionGUIDProperty.stringValue))
41+
{
42+
if (CollectionsRegistry.Instance.TryGetCollectionByGUID(collectionGUIDProperty.stringValue,
43+
out ScriptableObjectCollection collection))
3644
{
37-
cachedReference = collectable;
45+
if (collection.TryGetCollectableByGUID(collectableGUIDProperty.stringValue,
46+
out CollectableScriptableObject collectable))
47+
{
48+
objectAssetProperty.objectReferenceValue = collectable;
49+
objectAssetProperty.serializedObject.ApplyModifiedProperties();
50+
}
3851
}
3952
}
4053
}
41-
54+
55+
if (cachedReference == null && objectAssetProperty.objectReferenceValue != null)
56+
cachedReference = objectAssetProperty.objectReferenceValue as CollectableScriptableObject;
4257

4358
EditorGUI.PropertyField(position, objectAssetProperty, label, true);
44-
if (objectAssetProperty.exposedReferenceValue != cachedReference)
59+
if (objectAssetProperty.objectReferenceValue != cachedReference)
4560
{
4661
string collectableGUID = string.Empty;
4762
string collectionGUID = string.Empty;
@@ -61,6 +76,7 @@ public override void OnGUI(Rect position, SerializedProperty property, GUIConten
6176
collectableGUIDProperty.stringValue = collectableGUID;
6277
collectionGUIDProperty.stringValue = collectionGUID;
6378
objectAssetProperty.serializedObject.ApplyModifiedProperties();
79+
EditorUtility.SetDirty(objectAssetProperty.serializedObject.targetObject);
6480
}
6581
}
6682

Scripts/Editor/ScriptableObjectCollectionCustomEditor.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -411,8 +411,6 @@ private void DrawDeleteButton(CollectableScriptableObject item)
411411
collection.RemoveAt(index);
412412
AssetDatabase.DeleteAsset(AssetDatabase.GetAssetPath(item));
413413
ObjectUtility.SetDirty(collection);
414-
AssetDatabase.SaveAssets();
415-
AssetDatabase.Refresh();
416414
filteredItemListDirty = true;
417415
}
418416

Scripts/Runtime/CollectableIndirectReference.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,6 @@ namespace BrunoMikoski.ScriptableObjectCollections
66
[Serializable]
77
public abstract class CollectableIndirectReference
88
{
9-
#pragma warning disable 0649
10-
[SerializeField]
11-
private string name;
12-
#pragma warning restore 0649
13-
149
[SerializeField]
1510
protected string collectableGUID;
1611

Scripts/Runtime/Utils/AssetDatabaseUtils.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@ public static void RenameAsset(Object targetObject, string newName)
2424
targetObject.name = newName;
2525
UnityEditor.AssetDatabase.RenameAsset(UnityEditor.AssetDatabase.GetAssetPath(targetObject), newName);
2626
ObjectUtility.SetDirty(targetObject);
27-
UnityEditor.AssetDatabase.SaveAssets();
28-
UnityEditor.AssetDatabase.Refresh();
2927
#endif
3028
}
3129
}

0 commit comments

Comments
 (0)