Skip to content

Commit

Permalink
Merge pull request #165 from brunomikoski/fix/fix-on-enable-on-disabl…
Browse files Browse the repository at this point in the history
…e-not-protected

Fix on context menu and other small fixes
  • Loading branch information
brunomikoski authored Jan 30, 2025
2 parents c47dd8f + 95edb34 commit 33f4d64
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 33 deletions.
23 changes: 23 additions & 0 deletions CHANGELOG.MD
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
95 changes: 65 additions & 30 deletions Scripts/Editor/CustomEditors/CollectionCustomEditor.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
Expand Down Expand Up @@ -389,7 +390,7 @@ private void OnToggleExpand(MouseUpEvent evt)
expandShrinkButton.text = isOn.Value ? "▸◂" : "◂▸";
}

private void OnEnable()
protected virtual void OnEnable()
{
collection = (ScriptableObjectCollection)target;

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

private void OnDisable()
protected virtual void OnDisable()
{
SOCSettings.Instance.SaveCollectionSettings(collection, true);
}
Expand Down Expand Up @@ -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<ScriptableObject> itemsToBeDuplicated = new List<ScriptableObject>();
List<ScriptableObject> itemsToBeDeleted = new List<ScriptableObject>();
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();
Expand Down Expand Up @@ -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)
Expand All @@ -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);
}
Expand Down Expand Up @@ -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<ScriptableObject> itemsToBeDuplicated = new List<ScriptableObject>();
List<ScriptableObject> itemsToBeDeleted = new List<ScriptableObject>();
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);
}
}
);
Expand All @@ -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<ScriptableObject> moveItems =
new List<ScriptableObject>();
List<ScriptableObject> moveItems = new();
foreach (int selectedIndex in collectionItemListView.selectedIndices)
{
moveItems.Add(filteredItems[selectedIndex]);
Expand Down Expand Up @@ -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();

Expand Down
9 changes: 8 additions & 1 deletion Scripts/Runtime/Core/ScriptableObjectCollectionItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down Expand Up @@ -128,4 +135,4 @@ public override int GetHashCode()
return GUID.GetHashCode();
}
}
}
}
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "com.brunomikoski.scriptableobjectcollection",
"displayName": "Scriptable Object Collection",
"version": "2.3.7",
"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": [
Expand All @@ -28,4 +28,4 @@
"path": "Samples~/AddressablesCollection"
}
]
}
}

0 comments on commit 33f4d64

Please sign in to comment.