Skip to content

Commit aa42c48

Browse files
authored
Merge pull request #70 from brunomikoski/feature/setttings-revamp
Feature/setttings revamp
2 parents 3cbf1cd + 923fc20 commit aa42c48

File tree

7 files changed

+84
-131
lines changed

7 files changed

+84
-131
lines changed

CHANGELOG.MD

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,24 @@ All notable changes to this project will be documented in this file.
44
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

7+
8+
## [1.5.5]
9+
### Changed
10+
- In order to support multiple collections of the same type the check of the `Values` I had to tweak how the instance of every instance its assigned, changed for the `OnEnable` of the collection item
11+
- Changed the Refresh of the collections to only find items under the same folder.
12+
- Fixed a lot of scripts to work properly with multiple collections of the same type
13+
- Added some logs when `CollectionItems` are added/removed from collections to help the visibility
14+
- `CollectionType.Values` now will return all the `CollectionItem` available on the registry, so if you have 2 Collections of the one Type this will return the content of both collections while the `CollectionItem.Values` when the access file is generated will only return that specific collection items
15+
716
## [1.5.4]
817
### Changed
918
- Removed the automatically reload of CollectionRegistry using the `DidReloadScripts` callback, this caused multiple issues on batch mode and on clear libraries
1019
- Converted the `Collection` reference on the `CollectionItem` to be a Lazy reference, since this was causing some dependency when using `CollectionItem` as addressables
1120
- Refactored the `ScriptableObjectSettings` this used to be a Resources object that was auto loaded, but this could also create unwanted dependencies between items. The settings for code generated are not stored inside the Collection itself. Make sure you delete the `ScriptableObjectsSettings` ScriptableObject inside your resources folder.
1221
- Added a bunch of validation and verification for the Generation of the static access, making sure it only allows partial classes when is possible and other small checks.
1322
- The settings are now on the Project Preferences where you can define the default folder for the Scriptable Objects and default namespace.
23+
- Removed the `ReadOnlyList` on the `Collection`, the casting was expensive and was an unnecessary safety measure
24+
1425

1526
## [1.5.3]
1627
### Changed
@@ -229,7 +240,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
229240
### Unreleased
230241

231242

232-
243+
[1.5.5]: https://github.com/badawe/ScriptableObjectCollection/releases/tag/v1.5.5
233244
[1.5.4]: https://github.com/badawe/ScriptableObjectCollection/releases/tag/v1.5.4
234245
[1.5.3]: https://github.com/badawe/ScriptableObjectCollection/releases/tag/v1.5.3
235246
[1.5.2]: https://github.com/badawe/ScriptableObjectCollection/releases/tag/v1.5.2

Scripts/Editor/Core/CollectionCustomEditor.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public void OnEnable()
3535
{
3636
collection = (ScriptableObjectCollection)target;
3737

38-
if (!CollectionsRegistry.Instance.IsKnowCollectionGUID(collection.GUID))
38+
if (!CollectionsRegistry.Instance.IsKnowCollection(collection))
3939
CollectionsRegistry.Instance.ReloadCollections();
4040

4141
FixCollectionItems();
@@ -74,6 +74,11 @@ private void FixCollectionItems()
7474
return;
7575
}
7676
}
77+
78+
for (int i = 0; i < collection.Count; i++)
79+
{
80+
collection[i].SetCollection(collection);
81+
}
7782
}
7883

7984
public override void OnInspectorGUI()

Scripts/Editor/Core/CollectionsAssetsPostProcessor.cs

Lines changed: 13 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,39 +2,14 @@
22
using System.Collections.Generic;
33
using UnityEditor;
44
using UnityEditor.Callbacks;
5+
using UnityEngine;
56

67
namespace BrunoMikoski.ScriptableObjectCollections
78
{
89
public sealed class CollectionsAssetsPostProcessor : AssetPostprocessor
910
{
1011
private const string REFRESH_REGISTRY_AFTER_RECOMPILATION_KEY = "RefreshRegistryAfterRecompilationKey";
1112

12-
private static Dictionary<Type, ScriptableObjectCollection> cachedTypeToCollections;
13-
private static Dictionary<Type, ScriptableObjectCollection> typeToCollections
14-
{
15-
get
16-
{
17-
if (cachedTypeToCollections == null)
18-
{
19-
cachedTypeToCollections = new Dictionary<Type, ScriptableObjectCollection>();
20-
string[] collectionGUIDS = AssetDatabase.FindAssets($"t:{(nameof(ScriptableObjectCollection))}");
21-
22-
foreach (string guid in collectionGUIDS)
23-
{
24-
ScriptableObjectCollection collection =
25-
AssetDatabase.LoadAssetAtPath<ScriptableObjectCollection>(
26-
AssetDatabase.GUIDToAssetPath(guid));
27-
if(collection == null)
28-
continue;
29-
30-
cachedTypeToCollections.Add(collection.GetItemType(), collection);
31-
}
32-
}
33-
34-
return cachedTypeToCollections;
35-
}
36-
}
37-
3813
private static bool RefreshRegistryAfterRecompilation
3914
{
4015
get => EditorPrefs.GetBool(REFRESH_REGISTRY_AFTER_RECOMPILATION_KEY, false);
@@ -59,15 +34,19 @@ static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAsse
5934
{
6035
if (scriptableObjectCollectionItem.Collection == null)
6136
{
62-
if (typeToCollections.TryGetValue(type, out ScriptableObjectCollection collection))
63-
{
64-
if (!collection.Add(scriptableObjectCollectionItem))
65-
scriptableObjectCollectionItem.SetCollection(collection);
66-
}
37+
Debug.LogError(
38+
$"CollectionItem ({scriptableObjectCollectionItem.name}) has null Collection, please assign it some Collection",
39+
scriptableObjectCollectionItem
40+
);
6741
}
6842
else
6943
{
70-
scriptableObjectCollectionItem.Collection.Add(scriptableObjectCollectionItem);
44+
if (!scriptableObjectCollectionItem.Collection.Contains(scriptableObjectCollectionItem))
45+
{
46+
scriptableObjectCollectionItem.Collection.Add(scriptableObjectCollectionItem);
47+
Debug.Log($"{scriptableObjectCollectionItem.name} has collection assigned "
48+
+ $"{scriptableObjectCollectionItem.Collection} but its missing from collection list, adding it");
49+
}
7150
}
7251
}
7352
}
@@ -80,9 +59,10 @@ static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAsse
8059
if (collection == null)
8160
continue;
8261

83-
if (!CollectionsRegistry.Instance.IsKnowCollectionGUID(collection.GUID))
62+
if (!CollectionsRegistry.Instance.IsKnowCollection(collection))
8463
{
8564
RefreshRegistry();
65+
Debug.Log($"New collection found on the Project {collection.name}, refreshing the Registry");
8666
return;
8767
}
8868
}
@@ -98,7 +78,6 @@ private static void RefreshRegistry()
9878
}
9979

10080
EditorApplication.delayCall += () => { CollectionsRegistry.Instance.ReloadCollections(); };
101-
10281
}
10382

10483

Scripts/Runtime/Core/CollectionsRegistry.cs

Lines changed: 17 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Linq;
34
using BrunoMikoski.ScriptableObjectCollections.Core;
45
using UnityEngine;
56
#if UNITY_EDITOR
7+
using System.IO;
68
using UnityEditor;
79
#endif
810

@@ -15,20 +17,16 @@ public class CollectionsRegistry : ResourceScriptableObjectSingleton<Collections
1517
private List<ScriptableObjectCollection> collections = new List<ScriptableObjectCollection>();
1618
public IReadOnlyList<ScriptableObjectCollection> Collections => collections;
1719

18-
[SerializeField, HideInInspector]
19-
private List<string> collectionGUIDs = new List<string>();
20-
2120
public void UsedOnlyForAOTCodeGeneration()
2221
{
2322
LoadOrCreateInstance();
2423
// Include an exception so we can be sure to know if this method is ever called.
2524
throw new InvalidOperationException("This method is used for AOT code generation only. Do not call it at runtime.");
2625
}
2726

28-
public bool IsKnowCollectionGUID(string guid)
27+
public bool IsKnowCollection(ScriptableObjectCollection targetCollection)
2928
{
30-
ValidateCurrentGUIDs();
31-
return collectionGUIDs.Contains(guid);
29+
return collections.Any(collection => collection.GUID.Equals(targetCollection.GUID, StringComparison.Ordinal));
3230
}
3331

3432
public void RegisterCollection(ScriptableObjectCollection targetCollection)
@@ -37,7 +35,6 @@ public void RegisterCollection(ScriptableObjectCollection targetCollection)
3735
return;
3836

3937
collections.Add(targetCollection);
40-
collectionGUIDs.Add(targetCollection.GUID);
4138
}
4239

4340
public void UnregisterCollection(ScriptableObjectCollection targetCollection)
@@ -46,7 +43,6 @@ public void UnregisterCollection(ScriptableObjectCollection targetCollection)
4643
return;
4744

4845
collections.Remove(targetCollection);
49-
collectionGUIDs.Remove(targetCollection.GUID);
5046
}
5147

5248
private void ValidateItems()
@@ -56,39 +52,13 @@ private void ValidateItems()
5652
if (collections[i] == null)
5753
{
5854
collections.RemoveAt(i);
59-
collectionGUIDs.RemoveAt(i);
6055
}
6156
}
6257
}
63-
private void ValidateCurrentGUIDs()
64-
{
65-
ValidateItems();
66-
if (collectionGUIDs.Count != collections.Count)
67-
{
68-
ReloadCollections();
69-
return;
70-
}
7158

72-
for (int i = 0; i < collectionGUIDs.Count; i++)
73-
{
74-
string guid = collectionGUIDs[i];
75-
bool guidFound = false;
76-
for (int j = 0; j < collections.Count; j++)
77-
{
78-
ScriptableObjectCollection collection = collections[j];
79-
if (string.Equals(collection.GUID, guid, StringComparison.Ordinal))
80-
{
81-
guidFound = true;
82-
break;
83-
}
84-
}
85-
86-
if (!guidFound)
87-
{
88-
ReloadCollections();
89-
break;
90-
}
91-
}
59+
public List<T> GetAllCollectionItemsOfType<T>() where T : ScriptableObjectCollectionItem
60+
{
61+
return GetAllCollectionItemsOfType(typeof(T)).Cast<T>().ToList();
9262
}
9363

9464
public List<ScriptableObjectCollectionItem> GetAllCollectionItemsOfType(Type itemType)
@@ -162,17 +132,22 @@ public bool TryGetCollectionFromCollectableType(Type targetType, out ScriptableO
162132

163133
public bool TryGetCollectionFromItemType(Type targetType, out ScriptableObjectCollection scriptableObjectCollection)
164134
{
135+
List<ScriptableObjectCollection> possibleCollections = new List<ScriptableObjectCollection>();
165136
for (int i = 0; i < collections.Count; i++)
166137
{
167138
ScriptableObjectCollection collection = collections[i];
168-
if(collection.GetItemType() == targetType
169-
|| targetType.BaseType == collection.GetItemType())
139+
if (collection.GetItemType() == targetType || collection.GetItemType().IsAssignableFrom(targetType))
170140
{
171-
scriptableObjectCollection = collection;
172-
return true;
141+
possibleCollections.Add(collection);
173142
}
174143
}
175-
144+
145+
if (possibleCollections.Count == 1)
146+
{
147+
scriptableObjectCollection = possibleCollections.First();
148+
return true;
149+
}
150+
176151
scriptableObjectCollection = null;
177152
return false;
178153
}
@@ -231,7 +206,6 @@ public void DeleteCollection(ScriptableObjectCollection collection)
231206

232207
if (!collections.Remove(collection))
233208
return;
234-
collectionGUIDs.Remove(collection.GUID);
235209

236210
#if UNITY_EDITOR
237211
for (int i = collection.Items.Count - 1; i >= 0; i--)
@@ -247,7 +221,6 @@ public void ReloadCollections()
247221
return;
248222

249223
collections.Clear();
250-
collectionGUIDs.Clear();
251224

252225
bool changed = false;
253226
List<Type> types = TypeUtility.GetAllSubclasses(typeof(ScriptableObjectCollection));
@@ -267,7 +240,6 @@ public void ReloadCollections()
267240

268241
collection.RefreshCollection();
269242
collections.Add(collection);
270-
collectionGUIDs.Add(collection.GUID);
271243
changed = true;
272244
}
273245
}
@@ -299,7 +271,6 @@ public void RemoveNonAutomaticallyInitializedCollections()
299271
continue;
300272

301273
collections.Remove(collection);
302-
collectionGUIDs.Remove(collection.GUID);
303274
}
304275
ObjectUtility.SetDirty(this);
305276
#endif

0 commit comments

Comments
 (0)