Skip to content

Commit 5b6d4bc

Browse files
committed
fix: last known name usability
1 parent e496352 commit 5b6d4bc

3 files changed

Lines changed: 36 additions & 25 deletions

File tree

Scripts/Editor/PropertyDrawers/CollectionItemIndirectReferencePropertyDrawer.cs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ public override void OnGUI(Rect position, SerializedProperty property, GUIConten
5050
collectionLastKnownNameSerializedProperty = property.FindPropertyRelative(COLLECTION_LAST_KNOW_NAME_PROPERTY_PATH);
5151

5252
TryGetCollectionItem(out ScriptableObject collectionItem);
53-
53+
ValidateLastKnownNames(collectionItem);
54+
5455
int indexOfArrayPart = property.propertyPath.IndexOf('[');
5556
if (indexOfArrayPart > -1)
5657
{
@@ -132,6 +133,31 @@ private bool TryGetCollectionItem(out ScriptableObject item)
132133
return true;
133134
}
134135

136+
private void ValidateLastKnownNames(ScriptableObject collectionItem)
137+
{
138+
if (collectionItem == null || collectionItem is not ISOCItem socItem)
139+
return;
140+
141+
bool changed = false;
142+
143+
if (!string.Equals(itemLastKnowNameSerializedProperty.stringValue, socItem.name, StringComparison.Ordinal))
144+
{
145+
itemLastKnowNameSerializedProperty.stringValue = socItem.name;
146+
changed = true;
147+
}
148+
149+
if (!string.Equals(collectionLastKnownNameSerializedProperty.stringValue, socItem.Collection.name, StringComparison.Ordinal))
150+
{
151+
collectionLastKnownNameSerializedProperty.stringValue = socItem.Collection.name;
152+
changed = true;
153+
}
154+
155+
if (changed)
156+
{
157+
itemLastKnowNameSerializedProperty.serializedObject.ApplyModifiedPropertiesWithoutUndo();
158+
}
159+
}
160+
135161
private void CreateCollectionItemPropertyDrawer(SerializedProperty serializedProperty)
136162
{
137163
collectionItemPropertyDrawer = new CollectionItemPropertyDrawer();

Scripts/Editor/PropertyDrawers/CollectionItemPickerPropertyDrawer.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,9 @@ private void AssignItemGUIDToProperty(ScriptableObject scriptableObject, Seriali
204204

205205
newProperty.FindPropertyRelative(COLLECTION_GUID_VALUE_A).longValue = collectionValues.Item1;
206206
newProperty.FindPropertyRelative(COLLECTION_GUID_VALUE_B).longValue = collectionValues.Item2;
207+
208+
newProperty.FindPropertyRelative("itemLastKnownName").stringValue = item.name;
209+
newProperty.FindPropertyRelative("collectionLastKnownName").stringValue = item.Collection.name;
207210
}
208211

209212
private void SetSelectedValuesOnPopup(PopupList<PopupItem> popupList, SerializedProperty property)

Scripts/Runtime/Core/CollectionItemIndirectReference.cs

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,12 @@ public abstract class CollectionItemIndirectReference: IEquatable<CollectionItem
2020
protected long collectionGUIDValueB;
2121
protected LongGuid CollectionGUID => new LongGuid(collectionGUIDValueA, collectionGUIDValueB);
2222

23+
#if UNITY_EDITOR
2324
[SerializeField]
2425
protected string itemLastKnownName;
2526
[SerializeField, FormerlySerializedAs("collectionLastKnowName")]
2627
protected string collectionLastKnownName;
28+
#endif
2729

2830
public bool Equals(CollectionItemIndirectReference other)
2931
{
@@ -111,30 +113,6 @@ private bool TryResolveReference(out TObject result)
111113
return true;
112114
}
113115
}
114-
else
115-
{
116-
if (!string.IsNullOrEmpty(collectionLastKnownName))
117-
{
118-
if (CollectionsRegistry.Instance.TryGetCollectionByName(collectionLastKnownName, out collection))
119-
{
120-
SetCollection(collection);
121-
122-
if (!string.IsNullOrEmpty(itemLastKnownName))
123-
{
124-
if(collection.TryGetItemByName(itemLastKnownName, out ScriptableObject possibleResult))
125-
{
126-
result = possibleResult as TObject;
127-
if (result == null)
128-
{
129-
return false;
130-
}
131-
SetCollectionItem(result);
132-
return true;
133-
}
134-
}
135-
}
136-
}
137-
}
138116

139117
result = null;
140118
return false;
@@ -160,15 +138,19 @@ private void SetCollectionItem(ISOCItem item)
160138
(long, long) collectionItemValues = item.GUID.GetRawValues();
161139
collectionItemGUIDValueA = collectionItemValues.Item1;
162140
collectionItemGUIDValueB = collectionItemValues.Item2;
141+
#if UNITY_EDITOR
163142
itemLastKnownName = item.name;
143+
#endif
164144
}
165145

166146
public void SetCollection(ScriptableObjectCollection targetCollection)
167147
{
168148
(long,long) collectionGUIDValues = targetCollection.GUID.GetRawValues();
169149
collectionGUIDValueA = collectionGUIDValues.Item1;
170150
collectionGUIDValueB = collectionGUIDValues.Item2;
151+
#if UNITY_EDITOR
171152
collectionLastKnownName = targetCollection.name;
153+
#endif
172154
}
173155
}
174156
}

0 commit comments

Comments
 (0)