Skip to content

Commit 33f4d64

Browse files
authored
Merge pull request #165 from brunomikoski/fix/fix-on-enable-on-disable-not-protected
Fix on context menu and other small fixes
2 parents c47dd8f + 95edb34 commit 33f4d64

File tree

4 files changed

+98
-33
lines changed

4 files changed

+98
-33
lines changed

CHANGELOG.MD

+23
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,27 @@ 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
## [Unreleased]
8+
9+
## [2.3.8] - 30/01/2025
10+
## Changed
11+
- Fixed naming issue on the new Move Items context menu introduced on [2.3.7]
12+
- Renamed `Delete Item` to `Remove Item` on the context menu
13+
- 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
14+
- Exposed OnEnable and OnDisable from the CollectionCustomEditor to be overriden by other editors
15+
16+
## [2.3.7]
17+
## Added
18+
- Added new context menu to move items between collections
19+
- Added new Rename context menu
20+
- Added new CollectionItemQuery to be used with ItemPicker
21+
22+
## Changed
23+
- Improved Unity 6 support
24+
- Fixed null ref when collections settings where not set
25+
- Fixed some renaming issues
26+
- Changed event where non automatically loaded items are removed on editor simulation
27+
- Fixed null ref on empty collection
28+
829
## [2.3.6]
930
## Changed
1031
- Updated ListView to allow for multiple selection
@@ -591,6 +612,8 @@ public bool IsValidConsumable(Consumable consumable)
591612
### Added
592613
- First initial working version
593614

615+
[2.3.8]: https://github.com/badawe/ScriptableObjectCollection/releases/tag/v2.3.8
616+
[2.3.7]: https://github.com/badawe/ScriptableObjectCollection/releases/tag/v2.3.7
594617
[2.3.6]: https://github.com/badawe/ScriptableObjectCollection/releases/tag/v2.3.6
595618
[2.3.5]: https://github.com/badawe/ScriptableObjectCollection/releases/tag/v2.3.5
596619
[2.3.4]: https://github.com/badawe/ScriptableObjectCollection/releases/tag/v2.3.4

Scripts/Editor/CustomEditors/CollectionCustomEditor.cs

+65-30
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.IO;
34
using System.Linq;
45
using System.Text;
56
using System.Text.RegularExpressions;
@@ -389,7 +390,7 @@ private void OnToggleExpand(MouseUpEvent evt)
389390
expandShrinkButton.text = isOn.Value ? "▸◂" : "◂▸";
390391
}
391392

392-
private void OnEnable()
393+
protected virtual void OnEnable()
393394
{
394395
collection = (ScriptableObjectCollection)target;
395396

@@ -401,7 +402,7 @@ private void OnEnable()
401402
generator = generatorType == null ? null : CollectionGenerators.GetGenerator(generatorType);
402403
}
403404

404-
private void OnDisable()
405+
protected virtual void OnDisable()
405406
{
406407
SOCSettings.Instance.SaveCollectionSettings(collection, true);
407408
}
@@ -436,41 +437,46 @@ private void OnClickGenerateStaticFile(MouseUpEvent evt)
436437

437438
private void OnClickRemoveSelectedItems(MouseUpEvent evt)
438439
{
439-
440-
441440
if (!collectionItemListView.selectedIndices.Any())
442441
{
443-
if (!EditorUtility.DisplayDialog($"Delete Item",
444-
$"Are you sure you want to delete {filteredItems[^1].name}?", "Yes", "No"))
442+
int result = EditorUtility.DisplayDialogComplex($"Remove Item",
443+
$"Are you sure you want to remove the item {filteredItems[^1].name} from {collection.name}?", "Yes",
444+
"No", "Remove and Delete Asset");
445+
if (result == 1)
445446
{
446447
return;
447448
}
448449

449-
DeleteItemAtIndex(filteredItems.Count - 1);
450+
RemoveItemAtIndex(filteredItems.Count - 1, result == 2);
451+
ReloadFilteredItems();
450452
}
451453
else
452454
{
453-
if (!EditorUtility.DisplayDialog($"Delete {collectionItemListView.selectedIndices.Count()} Items",
454-
$"Are you sure you want to delete all {collectionItemListView.selectedIndices.Count()} items?", "Yes", "No"))
455+
int result = EditorUtility.DisplayDialogComplex("Remove Items",
456+
$"Are you sure you want to remove {collectionItemListView.selectedIndices.Count()} items from {collection.name}?", "Yes",
457+
"No", "Remove and Delete Asset");
458+
if (result == 1)
455459
{
456460
return;
457461
}
458462

459-
List<ScriptableObject> itemsToBeDuplicated = new List<ScriptableObject>();
463+
List<ScriptableObject> itemsToBeDeleted = new List<ScriptableObject>();
460464
foreach (int selectedIndex in collectionItemListView.selectedIndices)
461465
{
462-
itemsToBeDuplicated.Add(filteredItems[selectedIndex]);
466+
itemsToBeDeleted.Add(filteredItems[selectedIndex]);
463467
}
464468

465-
foreach (ScriptableObject item in itemsToBeDuplicated)
469+
foreach (ScriptableObject item in itemsToBeDeleted)
466470
{
467-
DeleteItemAtIndex(collection.IndexOf(item));
471+
RemoveItemAtIndex(collection.IndexOf(item), result == 2);
468472
}
473+
ReloadFilteredItems();
474+
469475
}
470476

471-
ReloadFilteredItems();
472477
}
473478

479+
474480
private void OnVisualTreeCreated()
475481
{
476482
UpdateHelpBox();
@@ -669,7 +675,7 @@ private void CreateAndAddNewItemOfType(Type itemSubClass)
669675
});
670676
}
671677

672-
protected void DeleteItemAtIndex(int selectedIndex)
678+
protected void RemoveItemAtIndex(int selectedIndex, bool deleteAsset)
673679
{
674680
ScriptableObject scriptableObject = filteredItems[selectedIndex];
675681
if (scriptableObject == null)
@@ -683,7 +689,15 @@ protected void DeleteItemAtIndex(int selectedIndex)
683689
filteredItems.Remove(scriptableObject);
684690
collection.Remove(scriptableObject);
685691

686-
AssetDatabase.DeleteAsset(AssetDatabase.GetAssetPath(scriptableObject));
692+
if (scriptableObject is ScriptableObjectCollectionItem socItem)
693+
{
694+
socItem.ClearCollection();
695+
}
696+
697+
if (deleteAsset)
698+
{
699+
AssetDatabase.DeleteAsset(AssetDatabase.GetAssetPath(scriptableObject));
700+
}
687701

688702
AssetDatabase.SaveAssetIfDirty(collection);
689703
}
@@ -787,38 +801,42 @@ private void ShowOptionsForIndex(MouseUpEvent evt, int targetIndex)
787801
);
788802

789803
menu.AddItem(
790-
new GUIContent("Delete Item"),
804+
new GUIContent("Remove Item"),
791805
false,
792806
() =>
793807
{
794808
if (selectedItemsCount > 0)
795809
{
796-
if (!EditorUtility.DisplayDialog($"Delete {collectionItemListView.selectedIndices.Count()} Items",
797-
$"Are you sure you want to delete all {collectionItemListView.selectedIndices.Count()} items?", "Yes", "No"))
810+
int result = EditorUtility.DisplayDialogComplex("Remove Items",
811+
$"Are you sure you want to remove {collectionItemListView.selectedIndices.Count()} items from {collection.name}?", "Yes",
812+
"No", "Remove and Delete Asset");
813+
if (result == 1)
798814
{
799815
return;
800816
}
801817

802-
List<ScriptableObject> itemsToBeDuplicated = new List<ScriptableObject>();
818+
List<ScriptableObject> itemsToBeDeleted = new List<ScriptableObject>();
803819
foreach (int selectedIndex in collectionItemListView.selectedIndices)
804820
{
805-
itemsToBeDuplicated.Add(filteredItems[selectedIndex]);
821+
itemsToBeDeleted.Add(filteredItems[selectedIndex]);
806822
}
807823

808-
foreach (ScriptableObject item in itemsToBeDuplicated)
824+
foreach (ScriptableObject item in itemsToBeDeleted)
809825
{
810-
DeleteItemAtIndex(collection.IndexOf(item));
826+
RemoveItemAtIndex(collection.IndexOf(item), result == 2);
811827
}
812828
}
813829
else
814830
{
815-
if (!EditorUtility.DisplayDialog($"Delete Item",
816-
$"Are you sure you want to delete {filteredItems[^1].name}?", "Yes", "No"))
831+
int result = EditorUtility.DisplayDialogComplex($"Remove Item",
832+
$"Are you sure you want to remove the item {filteredItems[^1].name} from {collection.name}?", "Yes",
833+
"No", "Remove and Delete Asset");
834+
if (result == 1)
817835
{
818836
return;
819837
}
820838

821-
DeleteItemAtIndex(targetIndex);
839+
RemoveItemAtIndex(filteredItems.Count - 1, result == 2);
822840
}
823841
}
824842
);
@@ -834,20 +852,19 @@ private void ShowOptionsForIndex(MouseUpEvent evt, int targetIndex)
834852
continue;
835853

836854
menu.AddItem(
837-
new GUIContent($"Move to {(AssetDatabase.GetAssetPath(scriptableObject).Replace("/","\\").Replace("Assets", "").Replace(".asset", ""))}"),
855+
new GUIContent($"Move to {(AssetDatabase.GetAssetPath(scriptableObjectCollection).Replace("/","\\").Replace("Assets", "").Replace(".asset", ""))}"),
838856
false,
839857
() =>
840858
{
841859
if (selectedItemsCount > 0)
842860
{
843861
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"))
862+
$"Are you sure you want to move {collectionItemListView.selectedIndices.Count()} items, from {AssetDatabase.GetAssetPath(collection)} to {AssetDatabase.GetAssetPath(scriptableObjectCollection)}", "Yes", "No"))
845863
{
846864
return;
847865
}
848866

849-
List<ScriptableObject> moveItems =
850-
new List<ScriptableObject>();
867+
List<ScriptableObject> moveItems = new();
851868
foreach (int selectedIndex in collectionItemListView.selectedIndices)
852869
{
853870
moveItems.Add(filteredItems[selectedIndex]);
@@ -914,6 +931,24 @@ private void MoveItem(ScriptableObject item, ScriptableObjectCollection targetCo
914931
collection.Remove(item);
915932
targetCollection.Add(item);
916933

934+
string itemPath = AssetDatabase.GetAssetPath(item);
935+
string targetCollectionPath = AssetDatabase.GetAssetPath(targetCollection);
936+
937+
if (!string.IsNullOrEmpty(itemPath) && !string.IsNullOrEmpty(targetCollectionPath))
938+
{
939+
string directory = Path.GetDirectoryName(targetCollectionPath);
940+
941+
string itemsFolderPath = Path.Combine(directory, "Items");
942+
bool hasItemsFolder = AssetDatabase.IsValidFolder(itemsFolderPath);
943+
944+
string finalDirectory = hasItemsFolder ? itemsFolderPath : directory;
945+
string fileName = Path.GetFileName(itemPath);
946+
947+
string newPath = AssetDatabase.GenerateUniqueAssetPath(Path.Combine(finalDirectory, fileName));
948+
949+
AssetDatabase.MoveAsset(itemPath, newPath);
950+
}
951+
917952
AssetDatabase.SaveAssets();
918953
AssetDatabase.Refresh();
919954

Scripts/Runtime/Core/ScriptableObjectCollectionItem.cs

+8-1
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,13 @@ public void SetCollection(ScriptableObjectCollection collection)
7575
collectionGUID = cachedCollection.GUID;
7676
ObjectUtility.SetDirty(this);
7777
}
78+
79+
public void ClearCollection()
80+
{
81+
cachedCollection = null;
82+
collectionGUID = default;
83+
ObjectUtility.SetDirty(this);
84+
}
7885

7986
public void GenerateNewGUID()
8087
{
@@ -128,4 +135,4 @@ public override int GetHashCode()
128135
return GUID.GetHashCode();
129136
}
130137
}
131-
}
138+
}

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "com.brunomikoski.scriptableobjectcollection",
33
"displayName": "Scriptable Object Collection",
4-
"version": "2.3.7",
4+
"version": "2.3.8",
55
"unity": "2022.2",
66
"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!",
77
"keywords": [
@@ -28,4 +28,4 @@
2828
"path": "Samples~/AddressablesCollection"
2929
}
3030
]
31-
}
31+
}

0 commit comments

Comments
 (0)