Skip to content

Commit 05f4f07

Browse files
authored
Merge pull request #167 from brunomikoski/feature/not-sure
Bug fixes and improvements
2 parents 33f4d64 + cf816b8 commit 05f4f07

20 files changed

+425
-90
lines changed

CHANGELOG.MD

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,19 @@ 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+
## [2.3.9] - 19/02/2025
9+
## Changed
10+
- Added some general fixes to validate Collection and Items have unique `LongGuid`
11+
- Fixed issues when after some operation with the context menu on the CollectionEditor items would not refresh properly
12+
- Fixed issue when moving items between collection would not generate unique names
13+
- Removed the post processing of assets that tried to assign items to collections automatically, since this was causing issue when duplicating collections, everyting now is done on the collection
14+
- Overall code improvement and organization
15+
16+
## Added
17+
- Added new Context Menu for moving items between collections
18+
- Added the new PropertyDrawer that shows what Collection every item belongs to
19+
- Added a few more Confirmation dialog when detecting items that looks wrong
20+
821

922
## [2.3.8] - 30/01/2025
1023
## Changed
@@ -612,6 +625,7 @@ public bool IsValidConsumable(Consumable consumable)
612625
### Added
613626
- First initial working version
614627

628+
[2.3.9]: https://github.com/badawe/ScriptableObjectCollection/releases/tag/v2.3.9
615629
[2.3.8]: https://github.com/badawe/ScriptableObjectCollection/releases/tag/v2.3.8
616630
[2.3.7]: https://github.com/badawe/ScriptableObjectCollection/releases/tag/v2.3.7
617631
[2.3.6]: https://github.com/badawe/ScriptableObjectCollection/releases/tag/v2.3.6

Scripts/Editor/CustomEditors/CollectionCustomEditor.cs

Lines changed: 25 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
namespace BrunoMikoski.ScriptableObjectCollections
2121
{
22-
2322
[CustomEditor(typeof(ScriptableObjectCollection), true)]
2423
public class CollectionCustomEditor : Editor
2524
{
@@ -394,6 +393,12 @@ protected virtual void OnEnable()
394393
{
395394
collection = (ScriptableObjectCollection)target;
396395

396+
if (!CollectionsRegistry.Instance.HasUniqueGUID(collection))
397+
{
398+
collection.GenerateNewGUID();
399+
collection.Clear();
400+
}
401+
397402
if (!CollectionsRegistry.Instance.IsKnowCollection(collection))
398403
CollectionsRegistry.Instance.ReloadCollections();
399404

@@ -626,10 +631,10 @@ private bool IsAddressableAsset(ScriptableObject target)
626631
#endif
627632
}
628633

629-
private ScriptableObject AddNewItemOfType(Type targetType, bool autoFocusForRename = true)
634+
private ScriptableObject AddNewItemOfType(Type targetType,string assetName = "", bool autoFocusForRename = true)
630635
{
631636
Undo.IncrementCurrentGroup();
632-
ScriptableObject newItem = collection.AddNew(targetType);
637+
ScriptableObject newItem = collection.AddNew(targetType, assetName);
633638
Undo.RegisterCreatedObjectUndo(newItem, "Create New Item");
634639
Undo.RecordObject(collection, "Add New Item");
635640
Undo.SetCurrentGroupName($"Created new item {newItem.name}");
@@ -792,10 +797,12 @@ private void ShowOptionsForIndex(MouseUpEvent evt, int targetIndex)
792797
{
793798
DuplicateItem(item, false);
794799
}
800+
ReloadFilteredItems();
795801
}
796802
else
797803
{
798804
DuplicateItem(targetIndex);
805+
ReloadFilteredItems();
799806
}
800807
}
801808
);
@@ -825,6 +832,7 @@ private void ShowOptionsForIndex(MouseUpEvent evt, int targetIndex)
825832
{
826833
RemoveItemAtIndex(collection.IndexOf(item), result == 2);
827834
}
835+
ReloadFilteredItems();
828836
}
829837
else
830838
{
@@ -837,6 +845,7 @@ private void ShowOptionsForIndex(MouseUpEvent evt, int targetIndex)
837845
}
838846

839847
RemoveItemAtIndex(filteredItems.Count - 1, result == 2);
848+
ReloadFilteredItems();
840849
}
841850
}
842851
);
@@ -872,19 +881,20 @@ private void ShowOptionsForIndex(MouseUpEvent evt, int targetIndex)
872881

873882
foreach (ScriptableObject item in moveItems)
874883
{
875-
MoveItem(item, scriptableObjectCollection);
884+
SOCItemUtility.MoveItem(item as ISOCItem, scriptableObjectCollection);
876885
}
886+
ReloadFilteredItems();
877887

878888
}
879889
else
880890
{
881-
if (!EditorUtility.DisplayDialog($"Move Item",
891+
if (!EditorUtility.DisplayDialog("Move Item",
882892
$"Are you sure you want to move {filteredItems[^1].name}, from {AssetDatabase.GetAssetPath(collection)} to {AssetDatabase.GetAssetPath(scriptableObject)}", "Yes", "No"))
883893
{
884894
return;
885895
}
886-
887-
MoveItem(filteredItems[targetIndex], scriptableObjectCollection);
896+
SOCItemUtility.MoveItem(filteredItems[targetIndex], scriptableObjectCollection);
897+
ReloadFilteredItems();
888898
}
889899
}
890900
);
@@ -923,38 +933,6 @@ private void ShowOptionsForIndex(MouseUpEvent evt, int targetIndex)
923933
menu.ShowAsContext();
924934
}
925935

926-
private void MoveItem(ScriptableObject item, ScriptableObjectCollection targetCollection)
927-
{
928-
Undo.RecordObject(collection, "Move Item");
929-
Undo.RecordObject(targetCollection, "Move Item");
930-
931-
collection.Remove(item);
932-
targetCollection.Add(item);
933-
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-
952-
AssetDatabase.SaveAssets();
953-
AssetDatabase.Refresh();
954-
955-
ReloadFilteredItems();
956-
}
957-
958936
private List<ScriptableObjectCollection> GetPossibleAnotherCollections()
959937
{
960938
CollectionsRegistry.Instance.TryGetCollectionsOfItemType(collection.GetItemType(), out List<ScriptableObjectCollection> collections);
@@ -980,7 +958,14 @@ private void DuplicateItem(int index, bool showRenameAfter = true)
980958
private void DuplicateItem(ScriptableObject source, bool showRenameAfter)
981959
{
982960
CopyCollectionItemUtility.SetSource(source);
983-
ScriptableObject newItem = AddNewItemOfType(source.GetType(), false);
961+
962+
string path = AssetDatabase.GetAssetPath(source);
963+
string directory = Path.GetDirectoryName(path);
964+
string fileName = $"{source.name} (Clone)";
965+
966+
fileName = AssetDatabase.GenerateUniqueAssetPath(Path.Combine(directory, $"{fileName}.asset"));
967+
968+
ScriptableObject newItem = AddNewItemOfType(source.GetType(), Path.GetFileNameWithoutExtension(fileName), false);
984969
CopyCollectionItemUtility.ApplySourceToTarget(newItem);
985970

986971
if (showRenameAfter)
@@ -990,7 +975,6 @@ private void DuplicateItem(ScriptableObject source, bool showRenameAfter)
990975
}
991976
else
992977
{
993-
AssetDatabaseUtils.RenameAsset(newItem, $"{source.name} (Copy)");
994978
AssetDatabase.SaveAssetIfDirty(newItem);
995979
ReloadFilteredItems();
996980
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using System.Collections.Generic;
2+
using UnityEditor;
3+
using UnityEngine;
4+
5+
namespace BrunoMikoski.ScriptableObjectCollections
6+
{
7+
public class MoveToCollectionWindow : EditorWindow
8+
{
9+
private List<ISOCItem> itemsToMove;
10+
private List<ScriptableObjectCollection> availableCollections;
11+
12+
public static void ShowWindow(List<ISOCItem> items, List<ScriptableObjectCollection> collections)
13+
{
14+
MoveToCollectionWindow window = GetWindow<MoveToCollectionWindow>("Move to Collection");
15+
window.itemsToMove = items;
16+
window.availableCollections = collections;
17+
window.ShowPopup();
18+
}
19+
20+
private void OnGUI()
21+
{
22+
EditorGUILayout.LabelField("Select a Collection to Move Items", EditorStyles.boldLabel);
23+
24+
if (availableCollections == null || availableCollections.Count == 0)
25+
{
26+
EditorGUILayout.LabelField("No available collections.");
27+
return;
28+
}
29+
30+
foreach (ScriptableObjectCollection collection in availableCollections)
31+
{
32+
if (GUILayout.Button(collection.name))
33+
{
34+
foreach (ISOCItem item in itemsToMove)
35+
{
36+
SOCItemUtility.MoveItem(item, collection);
37+
EditorUtility.SetDirty(collection);
38+
}
39+
40+
Close();
41+
}
42+
}
43+
}
44+
}
45+
}

Scripts/Editor/CustomEditors/MoveToCollectionWindow.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Scripts/Editor/Processors/CollectionAssetsModificationProcessor.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ public static AssetDeleteResult OnWillDeleteAsset(string targetAssetPath, Remove
2424
if (socItem == null)
2525
return AssetDeleteResult.DidNotDelete;
2626

27+
if (socItem.Collection == null)
28+
return AssetDeleteResult.DidNotDelete;
29+
2730
socItem.Collection.Remove(collectionItem);
2831
return AssetDeleteResult.DidNotDelete;
2932
}
@@ -42,4 +45,4 @@ public static AssetDeleteResult OnWillDeleteAsset(string targetAssetPath, Remove
4245
}
4346

4447
}
45-
}
48+
}

Scripts/Editor/Processors/CollectionsAssetsPostProcessor.cs

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -29,30 +29,21 @@ static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAsse
2929
ScriptableObject collectionItem =
3030
AssetDatabase.LoadAssetAtPath<ScriptableObject>(importedAssetPath);
3131

32-
if (collectionItem != null)
32+
if (collectionItem == null)
3333
{
34-
if (collectionItem is ISOCItem socItem)
35-
{
36-
if (socItem.Collection == null)
37-
{
38-
continue;
39-
}
34+
continue;
35+
}
36+
37+
if (collectionItem is not ISOCItem socItem)
38+
{
39+
continue;
40+
}
4041

41-
if (!socItem.Collection.Contains(collectionItem))
42-
{
43-
if (socItem.Collection.TryGetItemByGUID(socItem.GUID, out _))
44-
{
45-
Debug.LogWarning(
46-
$"Collection already contains one item with the same GUID" +
47-
$" ({socItem.GUID}) but different name ({socItem.name}), generating new GUID");
48-
socItem.GenerateNewGUID();
49-
}
50-
51-
socItem.Collection.Add(collectionItem);
52-
Debug.Log($"{collectionItem.name} has collection assigned "
53-
+ $"{socItem.Collection} but its missing from collection list, adding it");
54-
}
55-
}
42+
if (!CollectionsRegistry.Instance.HasUniqueGUID(socItem))
43+
{
44+
socItem.GenerateNewGUID();
45+
socItem.ClearCollection();
46+
Debug.LogWarning($"Item {socItem} GUID was not unique, generating a new one and clearing the collection");
5647
}
5748
}
5849

@@ -64,6 +55,13 @@ static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAsse
6455
if (collection == null)
6556
continue;
6657

58+
if (!CollectionsRegistry.Instance.HasUniqueGUID(collection))
59+
{
60+
collection.GenerateNewGUID();
61+
collection.Clear();
62+
Debug.LogWarning($"Collection {collection} GUID was not unique, generating a new one, and clearing the items");
63+
}
64+
6765
if (!CollectionsRegistry.Instance.IsKnowCollection(collection))
6866
{
6967
RefreshRegistry();
@@ -95,4 +93,4 @@ static void OnAfterScriptsReloading()
9593
RefreshRegistryAfterRecompilation = false;
9694
}
9795
}
98-
}
96+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using UnityEditor;
2+
using UnityEngine;
3+
4+
namespace BrunoMikoski.ScriptableObjectCollections.Picker
5+
{
6+
[CustomPropertyDrawer(typeof(CollectionReferenceLongGuidAttribute))]
7+
public class CollectionReferenceLongGuidDrawer : PropertyDrawer
8+
{
9+
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
10+
{
11+
return EditorGUIUtility.singleLineHeight;
12+
}
13+
14+
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
15+
{
16+
LongGuid collectionGUID = (LongGuid)property.boxedValue;
17+
18+
ScriptableObjectCollection collection = null;
19+
if (collectionGUID.IsValid())
20+
{
21+
collection = CollectionsRegistry.Instance.GetCollectionByGUID(collectionGUID);
22+
}
23+
24+
EditorGUI.BeginDisabledGroup(true);
25+
EditorGUI.ObjectField(position, "Collection", collection, typeof(ScriptableObjectCollection), false);
26+
EditorGUI.EndDisabledGroup();
27+
}
28+
}
29+
}

Scripts/Editor/PropertyDrawers/CollectionReferenceLongGuidDrawer.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)