From 230d890cf1d01231ad46a568b19e9adec346ffa9 Mon Sep 17 00:00:00 2001 From: Bruno Mikoski Date: Thu, 30 Jan 2025 21:08:44 +0000 Subject: [PATCH] add: more fixes to rename, delete and move context menu actions --- CHANGELOG.MD | 23 +++++ .../CustomEditors/CollectionCustomEditor.cs | 91 +++++++++++++------ .../Core/ScriptableObjectCollectionItem.cs | 9 +- package.json | 2 +- 4 files changed, 95 insertions(+), 30 deletions(-) diff --git a/CHANGELOG.MD b/CHANGELOG.MD index 5252282..10964e5 100644 --- a/CHANGELOG.MD +++ b/CHANGELOG.MD @@ -5,6 +5,27 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## [Unreleased] + +## [2.3.8] - 30/01/2025 +## Changed + - Fixed naming issue on the new Move Items context menu introduced on [2.3.7] + - Renamed `Delete Item` to `Remove Item` on the context menu + - Updated the Confirmation for the Removal of items, now ask if you just want to remove the reference, remove the reference and delete the asset or cancel the operation + - Exposed OnEnable and OnDisable from the CollectionCustomEditor to be overriden by other editors + +## [2.3.7] +## Added +- Added new context menu to move items between collections +- Added new Rename context menu +- Added new CollectionItemQuery to be used with ItemPicker + +## Changed +- Improved Unity 6 support +- Fixed null ref when collections settings where not set +- Fixed some renaming issues +- Changed event where non automatically loaded items are removed on editor simulation +- Fixed null ref on empty collection + ## [2.3.6] ## Changed - Updated ListView to allow for multiple selection @@ -591,6 +612,8 @@ public bool IsValidConsumable(Consumable consumable) ### Added - First initial working version +[2.3.8]: https://github.com/badawe/ScriptableObjectCollection/releases/tag/v2.3.8 +[2.3.7]: https://github.com/badawe/ScriptableObjectCollection/releases/tag/v2.3.7 [2.3.6]: https://github.com/badawe/ScriptableObjectCollection/releases/tag/v2.3.6 [2.3.5]: https://github.com/badawe/ScriptableObjectCollection/releases/tag/v2.3.5 [2.3.4]: https://github.com/badawe/ScriptableObjectCollection/releases/tag/v2.3.4 diff --git a/Scripts/Editor/CustomEditors/CollectionCustomEditor.cs b/Scripts/Editor/CustomEditors/CollectionCustomEditor.cs index 70f418f..3225e4e 100644 --- a/Scripts/Editor/CustomEditors/CollectionCustomEditor.cs +++ b/Scripts/Editor/CustomEditors/CollectionCustomEditor.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.IO; using System.Linq; using System.Text; using System.Text.RegularExpressions; @@ -436,41 +437,46 @@ private void OnClickGenerateStaticFile(MouseUpEvent evt) private void OnClickRemoveSelectedItems(MouseUpEvent evt) { - - if (!collectionItemListView.selectedIndices.Any()) { - if (!EditorUtility.DisplayDialog($"Delete Item", - $"Are you sure you want to delete {filteredItems[^1].name}?", "Yes", "No")) + int result = EditorUtility.DisplayDialogComplex($"Remove Item", + $"Are you sure you want to remove the item {filteredItems[^1].name} from {collection.name}?", "Yes", + "No", "Remove and Delete Asset"); + if (result == 1) { return; } - DeleteItemAtIndex(filteredItems.Count - 1); + RemoveItemAtIndex(filteredItems.Count - 1, result == 2); + ReloadFilteredItems(); } else { - if (!EditorUtility.DisplayDialog($"Delete {collectionItemListView.selectedIndices.Count()} Items", - $"Are you sure you want to delete all {collectionItemListView.selectedIndices.Count()} items?", "Yes", "No")) + int result = EditorUtility.DisplayDialogComplex("Remove Items", + $"Are you sure you want to remove {collectionItemListView.selectedIndices.Count()} items from {collection.name}?", "Yes", + "No", "Remove and Delete Asset"); + if (result == 1) { return; } - List itemsToBeDuplicated = new List(); + List itemsToBeDeleted = new List(); foreach (int selectedIndex in collectionItemListView.selectedIndices) { - itemsToBeDuplicated.Add(filteredItems[selectedIndex]); + itemsToBeDeleted.Add(filteredItems[selectedIndex]); } - foreach (ScriptableObject item in itemsToBeDuplicated) + foreach (ScriptableObject item in itemsToBeDeleted) { - DeleteItemAtIndex(collection.IndexOf(item)); + RemoveItemAtIndex(collection.IndexOf(item), result == 2); } + ReloadFilteredItems(); + } - ReloadFilteredItems(); } + private void OnVisualTreeCreated() { UpdateHelpBox(); @@ -669,7 +675,7 @@ private void CreateAndAddNewItemOfType(Type itemSubClass) }); } - protected void DeleteItemAtIndex(int selectedIndex) + protected void RemoveItemAtIndex(int selectedIndex, bool deleteAsset) { ScriptableObject scriptableObject = filteredItems[selectedIndex]; if (scriptableObject == null) @@ -683,7 +689,15 @@ protected void DeleteItemAtIndex(int selectedIndex) filteredItems.Remove(scriptableObject); collection.Remove(scriptableObject); - AssetDatabase.DeleteAsset(AssetDatabase.GetAssetPath(scriptableObject)); + if (scriptableObject is ScriptableObjectCollectionItem socItem) + { + socItem.ClearCollection(); + } + + if (deleteAsset) + { + AssetDatabase.DeleteAsset(AssetDatabase.GetAssetPath(scriptableObject)); + } AssetDatabase.SaveAssetIfDirty(collection); } @@ -787,38 +801,42 @@ private void ShowOptionsForIndex(MouseUpEvent evt, int targetIndex) ); menu.AddItem( - new GUIContent("Delete Item"), + new GUIContent("Remove Item"), false, () => { if (selectedItemsCount > 0) { - if (!EditorUtility.DisplayDialog($"Delete {collectionItemListView.selectedIndices.Count()} Items", - $"Are you sure you want to delete all {collectionItemListView.selectedIndices.Count()} items?", "Yes", "No")) + int result = EditorUtility.DisplayDialogComplex("Remove Items", + $"Are you sure you want to remove {collectionItemListView.selectedIndices.Count()} items from {collection.name}?", "Yes", + "No", "Remove and Delete Asset"); + if (result == 1) { return; } - List itemsToBeDuplicated = new List(); + List itemsToBeDeleted = new List(); foreach (int selectedIndex in collectionItemListView.selectedIndices) { - itemsToBeDuplicated.Add(filteredItems[selectedIndex]); + itemsToBeDeleted.Add(filteredItems[selectedIndex]); } - foreach (ScriptableObject item in itemsToBeDuplicated) + foreach (ScriptableObject item in itemsToBeDeleted) { - DeleteItemAtIndex(collection.IndexOf(item)); + RemoveItemAtIndex(collection.IndexOf(item), result == 2); } } else { - if (!EditorUtility.DisplayDialog($"Delete Item", - $"Are you sure you want to delete {filteredItems[^1].name}?", "Yes", "No")) + int result = EditorUtility.DisplayDialogComplex($"Remove Item", + $"Are you sure you want to remove the item {filteredItems[^1].name} from {collection.name}?", "Yes", + "No", "Remove and Delete Asset"); + if (result == 1) { return; } - DeleteItemAtIndex(targetIndex); + RemoveItemAtIndex(filteredItems.Count - 1, result == 2); } } ); @@ -834,20 +852,19 @@ private void ShowOptionsForIndex(MouseUpEvent evt, int targetIndex) continue; menu.AddItem( - new GUIContent($"Move to {(AssetDatabase.GetAssetPath(scriptableObject).Replace("/","\\").Replace("Assets", "").Replace(".asset", ""))}"), + new GUIContent($"Move to {(AssetDatabase.GetAssetPath(scriptableObjectCollection).Replace("/","\\").Replace("Assets", "").Replace(".asset", ""))}"), false, () => { if (selectedItemsCount > 0) { if (!EditorUtility.DisplayDialog($"Move {collectionItemListView.selectedIndices.Count()} Items", - $"Are you sure you want to move {collectionItemListView.selectedIndices.Count()} items, from {AssetDatabase.GetAssetPath(collection)} to {AssetDatabase.GetAssetPath(scriptableObject)}", "Yes", "No")) + $"Are you sure you want to move {collectionItemListView.selectedIndices.Count()} items, from {AssetDatabase.GetAssetPath(collection)} to {AssetDatabase.GetAssetPath(scriptableObjectCollection)}", "Yes", "No")) { return; } - List moveItems = - new List(); + List moveItems = new(); foreach (int selectedIndex in collectionItemListView.selectedIndices) { moveItems.Add(filteredItems[selectedIndex]); @@ -914,6 +931,24 @@ private void MoveItem(ScriptableObject item, ScriptableObjectCollection targetCo collection.Remove(item); targetCollection.Add(item); + string itemPath = AssetDatabase.GetAssetPath(item); + string targetCollectionPath = AssetDatabase.GetAssetPath(targetCollection); + + if (!string.IsNullOrEmpty(itemPath) && !string.IsNullOrEmpty(targetCollectionPath)) + { + string directory = Path.GetDirectoryName(targetCollectionPath); + + string itemsFolderPath = Path.Combine(directory, "Items"); + bool hasItemsFolder = AssetDatabase.IsValidFolder(itemsFolderPath); + + string finalDirectory = hasItemsFolder ? itemsFolderPath : directory; + string fileName = Path.GetFileName(itemPath); + + string newPath = AssetDatabase.GenerateUniqueAssetPath(Path.Combine(finalDirectory, fileName)); + + AssetDatabase.MoveAsset(itemPath, newPath); + } + AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); diff --git a/Scripts/Runtime/Core/ScriptableObjectCollectionItem.cs b/Scripts/Runtime/Core/ScriptableObjectCollectionItem.cs index 6975286..ed75e14 100644 --- a/Scripts/Runtime/Core/ScriptableObjectCollectionItem.cs +++ b/Scripts/Runtime/Core/ScriptableObjectCollectionItem.cs @@ -75,6 +75,13 @@ public void SetCollection(ScriptableObjectCollection collection) collectionGUID = cachedCollection.GUID; ObjectUtility.SetDirty(this); } + + public void ClearCollection() + { + cachedCollection = null; + collectionGUID = default; + ObjectUtility.SetDirty(this); + } public void GenerateNewGUID() { @@ -128,4 +135,4 @@ public override int GetHashCode() return GUID.GetHashCode(); } } -} +} \ No newline at end of file diff --git a/package.json b/package.json index f2363fc..f385dc2 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "com.brunomikoski.scriptableobjectcollection", "displayName": "Scriptable Object Collection", - "version": "2.3.6", + "version": "2.3.8", "unity": "2022.2", "description": "A library to help improve the usability of Unity3D Scriptable Objects by grouping them into a collection and exposing them by code or nice inspectors!", "keywords": [