Skip to content

Commit b2e2936

Browse files
authored
Merge pull request #162 from brunomikoski/feature/unity-6-support
Feature/unity 6 support
2 parents ffb1461 + 254dd0f commit b2e2936

File tree

9 files changed

+342
-35
lines changed

9 files changed

+342
-35
lines changed

Scripts/Editor/Core/EditorBehaviour.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ static EditorBehaviour()
1212

1313
private static void OnPlayModeStateChanged(PlayModeStateChange playModeStateChange)
1414
{
15-
if (playModeStateChange == PlayModeStateChange.EnteredPlayMode)
15+
if (playModeStateChange == PlayModeStateChange.ExitingEditMode)
1616
{
1717
CollectionsRegistry.Instance.RemoveNonAutomaticallyInitializedCollections();
1818
}

Scripts/Editor/Core/SOCSettings.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -262,10 +262,8 @@ public CollectionSettings GetOrCreateCollectionSettings(ScriptableObjectCollecti
262262
string path = AssetDatabase.GetAssetPath(collection);
263263
AssetImporter importer = AssetImporter.GetAtPath(path);
264264

265-
string collectionSettingsData = importer.userData;
266-
267265
CollectionSettings collectionSetting;
268-
if (string.IsNullOrEmpty(collectionSettingsData))
266+
if (importer == null || string.IsNullOrEmpty(importer.userData))
269267
{
270268
collectionSetting = new CollectionSettings(collection);
271269
}
@@ -302,4 +300,4 @@ public void SaveCollectionSettings(ScriptableObjectCollection collection, bool f
302300
settings.Save();
303301
}
304302
}
305-
}
303+
}

Scripts/Editor/CustomEditors/CollectionCustomEditor.cs

Lines changed: 100 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -823,6 +823,57 @@ private void ShowOptionsForIndex(MouseUpEvent evt, int targetIndex)
823823
}
824824
);
825825

826+
827+
List<ScriptableObjectCollection> possibleAnotherCollections = GetPossibleAnotherCollections();
828+
829+
if (possibleAnotherCollections.Count > 0)
830+
{
831+
foreach (ScriptableObjectCollection scriptableObjectCollection in possibleAnotherCollections)
832+
{
833+
if (scriptableObjectCollection == collection)
834+
continue;
835+
836+
menu.AddItem(
837+
new GUIContent($"Move to {(AssetDatabase.GetAssetPath(scriptableObject).Replace("/","\\").Replace("Assets", "").Replace(".asset", ""))}"),
838+
false,
839+
() =>
840+
{
841+
if (selectedItemsCount > 0)
842+
{
843+
if (!EditorUtility.DisplayDialog($"Move {collectionItemListView.selectedIndices.Count()} Items",
844+
$"Are you sure you want to move {collectionItemListView.selectedIndices.Count()} items, from {AssetDatabase.GetAssetPath(collection)} to {AssetDatabase.GetAssetPath(scriptableObject)}", "Yes", "No"))
845+
{
846+
return;
847+
}
848+
849+
List<ScriptableObject> moveItems =
850+
new List<ScriptableObject>();
851+
foreach (int selectedIndex in collectionItemListView.selectedIndices)
852+
{
853+
moveItems.Add(filteredItems[selectedIndex]);
854+
}
855+
856+
foreach (ScriptableObject item in moveItems)
857+
{
858+
MoveItem(item, scriptableObjectCollection);
859+
}
860+
861+
}
862+
else
863+
{
864+
if (!EditorUtility.DisplayDialog($"Move Item",
865+
$"Are you sure you want to move {filteredItems[^1].name}, from {AssetDatabase.GetAssetPath(collection)} to {AssetDatabase.GetAssetPath(scriptableObject)}", "Yes", "No"))
866+
{
867+
return;
868+
}
869+
870+
MoveItem(filteredItems[targetIndex], scriptableObjectCollection);
871+
}
872+
}
873+
);
874+
}
875+
}
876+
826877
menu.AddSeparator("");
827878
menu.AddItem(
828879
new GUIContent("Select Asset"),
@@ -840,9 +891,41 @@ private void ShowOptionsForIndex(MouseUpEvent evt, int targetIndex)
840891
}
841892
);
842893

894+
if (selectedItemsCount == 1)
895+
{
896+
menu.AddItem(
897+
new GUIContent("Rename Asset"),
898+
false,
899+
() =>
900+
{
901+
RenameItemAtIndex(collectionItemListView.selectedIndices.First());
902+
}
903+
);
904+
}
905+
843906
menu.ShowAsContext();
844907
}
845908

909+
private void MoveItem(ScriptableObject item, ScriptableObjectCollection targetCollection)
910+
{
911+
Undo.RecordObject(collection, "Move Item");
912+
Undo.RecordObject(targetCollection, "Move Item");
913+
914+
collection.Remove(item);
915+
targetCollection.Add(item);
916+
917+
AssetDatabase.SaveAssets();
918+
AssetDatabase.Refresh();
919+
920+
ReloadFilteredItems();
921+
}
922+
923+
private List<ScriptableObjectCollection> GetPossibleAnotherCollections()
924+
{
925+
CollectionsRegistry.Instance.TryGetCollectionsOfItemType(collection.GetItemType(), out List<ScriptableObjectCollection> collections);
926+
return collections;
927+
}
928+
846929
private void SelectItemAtIndex(params int[] index)
847930
{
848931
Object[] selectedObjects = new Object[index.Length];
@@ -885,7 +968,7 @@ private void RenameItemAtIndex(MouseDownEvent evt, int targetIndex)
885968

886969
private void RenameItemAtIndex(int targetIndex)
887970
{
888-
ClearCurrentRenamingItem();
971+
ClearCurrentRenamingItem(false);
889972

890973
Undo.RecordObject(filteredItems[targetIndex], "Rename Item");
891974
VisualElement targetElement = collectionItemListView.GetRootElementForIndex(targetIndex);
@@ -895,19 +978,24 @@ private void RenameItemAtIndex(int targetIndex)
895978
currentRenamingLabel.style.display = DisplayStyle.None;
896979

897980
currentRenamingTextField = targetElement.Q<TextField>();
898-
currentRenamingTextField.RegisterCallback<FocusOutEvent>(OnRenamingAssetLostFocus);
899-
currentRenamingTextField.RegisterValueChangedCallback(_ => OnFinishRenamingItem(targetIndex));
900981

901982
currentRenamingTextField.SetValueWithoutNotify(currentRenamingLabel.text);
902983
currentRenamingTextField.style.display = DisplayStyle.Flex;
903984
currentRenamingTextField.SelectAll();
904985
currentRenamingTextField.Focus();
905986
collectionItemListView.ClearSelection();
987+
988+
currentRenamingTextField.schedule.Execute(() =>
989+
{
990+
currentRenamingTextField.SelectAll();
991+
currentRenamingTextField.RegisterCallback<FocusOutEvent>(OnRenamingAssetLostFocus);
992+
currentRenamingTextField.RegisterValueChangedCallback(_ => OnFinishRenamingItem(targetIndex));
993+
}).ExecuteLater(0);
906994
}
907995

908996
private void OnRenamingAssetLostFocus(FocusOutEvent evt)
909997
{
910-
ClearCurrentRenamingItem();
998+
ClearCurrentRenamingItem(false);
911999
}
9121000

9131001
private void OnFinishRenamingItem(int targetIndex)
@@ -929,18 +1017,22 @@ private void OnFinishRenamingItem(int targetIndex)
9291017
AssetDatabase.SaveAssetIfDirty(asset);
9301018
}
9311019

932-
ClearCurrentRenamingItem();
1020+
ClearCurrentRenamingItem(true);
9331021
}
9341022

935-
private void ClearCurrentRenamingItem()
1023+
private void ClearCurrentRenamingItem(bool renamedSuccessfully)
9361024
{
9371025
if (currentRenamingTextField == null)
9381026
return;
9391027

1028+
currentRenamingTextField.UnregisterCallback<FocusOutEvent>(OnRenamingAssetLostFocus);
9401029
currentRenamingTextField.style.display = DisplayStyle.None;
9411030
currentRenamingLabel.style.display = DisplayStyle.Flex;
942-
currentRenamingLabel.text = currentRenamingTextField.text;
943-
currentRenamingTextField.SetValueWithoutNotify("");
1031+
if (renamedSuccessfully)
1032+
{
1033+
currentRenamingLabel.text = currentRenamingTextField.text;
1034+
currentRenamingTextField.SetValueWithoutNotify("");
1035+
}
9441036
currentRenamingLabel = null;
9451037
currentRenamingTextField = null;
9461038
}

Scripts/Editor/PropertyDrawers/CollectionItemPickerPropertyDrawer.cs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,8 @@ private void Initialize(SerializedProperty property)
265265
if (initializedPropertiesPaths.Contains(property.propertyPath))
266266
return;
267267

268+
ValidateIndirectReferencesInProperty(property);
269+
268270
Type arrayOrListType = fieldInfo.FieldType.GetArrayOrListType();
269271
Type itemType = arrayOrListType ?? fieldInfo.FieldType;
270272

@@ -297,5 +299,46 @@ private void Initialize(SerializedProperty property)
297299
labelStyle = assetLabelStyle;
298300
initializedPropertiesPaths.Add(property.propertyPath);
299301
}
302+
303+
private void ValidateIndirectReferencesInProperty(SerializedProperty property)
304+
{
305+
SerializedProperty indirectReferencesProperty = property.FindPropertyRelative(ITEMS_PROPERTY_NAME);
306+
307+
bool changed = false;
308+
for (int i = indirectReferencesProperty.arraySize - 1; i >= 0; i--)
309+
{
310+
SerializedProperty elementProperty = indirectReferencesProperty.GetArrayElementAtIndex(i);
311+
312+
long collectionGUIDValueA = elementProperty.FindPropertyRelative(COLLECTION_GUID_VALUE_A).longValue;
313+
long collectionGUIDValueB = elementProperty.FindPropertyRelative(COLLECTION_GUID_VALUE_B).longValue;
314+
LongGuid collectionGUID = new(collectionGUIDValueA, collectionGUIDValueB);
315+
316+
long itemGUIDValueA = elementProperty.FindPropertyRelative(COLLECTION_ITEM_GUID_VALUE_A).longValue;
317+
long itemGUIDValueB = elementProperty.FindPropertyRelative(COLLECTION_ITEM_GUID_VALUE_B).longValue;
318+
LongGuid itemGUID = new(itemGUIDValueA, itemGUIDValueB);
319+
320+
bool validReference = false;
321+
if(CollectionsRegistry.Instance.TryGetCollectionByGUID(collectionGUID, out ScriptableObjectCollection collection))
322+
{
323+
if (collection.TryGetItemByGUID(itemGUID, out _))
324+
{
325+
validReference = true;
326+
}
327+
}
328+
329+
if (!validReference)
330+
{
331+
indirectReferencesProperty.DeleteArrayElementAtIndex(i);
332+
changed = true;
333+
}
334+
}
335+
336+
if (changed)
337+
{
338+
indirectReferencesProperty.serializedObject.ApplyModifiedProperties();
339+
}
340+
}
341+
342+
300343
}
301344
}

Scripts/Runtime/Core/CollectionItemIndirectReference.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public TObject Ref
8585

8686
private bool TryResolveReference(out TObject result)
8787
{
88-
if (CollectionsRegistry.Instance.TryGetCollectionByGUID(CollectionGUID, out ScriptableObjectCollection<TObject> collection))
88+
if (CollectionsRegistry.Instance.TryGetCollectionByGUID(CollectionGUID, out ScriptableObjectCollection collection))
8989
{
9090
if (collection.TryGetItemByGUID(CollectionItemGUID, out ScriptableObject item))
9191
{
@@ -103,8 +103,13 @@ private bool TryResolveReference(out TObject result)
103103

104104
if (!string.IsNullOrEmpty(itemLastKnownName))
105105
{
106-
if(collection.TryGetItemByName(itemLastKnownName, out result))
106+
if(collection.TryGetItemByName(itemLastKnownName, out ScriptableObject possibleResult))
107107
{
108+
result = possibleResult as TObject;
109+
if (result == null)
110+
{
111+
return false;
112+
}
108113
SetCollectionItem(result);
109114
return true;
110115
}
@@ -148,4 +153,4 @@ public void SetCollection(ScriptableObjectCollection targetCollection)
148153
collectionLastKnowName = targetCollection.name;
149154
}
150155
}
151-
}
156+
}

Scripts/Runtime/Core/CollectionItemPicker.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,14 @@ public List<TItemType> Items
3333

3434
for (int i = 0; i < indirectReferences.Count; i++)
3535
{
36-
cachedItems.Add(indirectReferences[i].Ref);
36+
CollectionItemIndirectReference<TItemType> collectionItemIndirectReference = indirectReferences[i];
37+
if (!collectionItemIndirectReference.IsValid() || collectionItemIndirectReference.Ref == null)
38+
{
39+
indirectReferences.RemoveAt(i);
40+
continue;
41+
}
42+
43+
cachedItems.Add(collectionItemIndirectReference.Ref);
3744
}
3845

3946
isDirty = false;

0 commit comments

Comments
 (0)