Skip to content

Commit 8540d54

Browse files
committed
add: guid lockup for collections
1 parent 40553af commit 8540d54

File tree

1 file changed

+29
-4
lines changed

1 file changed

+29
-4
lines changed

Scripts/Runtime/Core/CollectionsRegistry.cs

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ public class CollectionsRegistry : ResourceScriptableObjectSingleton<Collections
2020
[SerializeField]
2121
private List<ScriptableObjectCollection> collections = new List<ScriptableObjectCollection>();
2222
public IReadOnlyList<ScriptableObjectCollection> Collections => collections;
23-
23+
24+
private Dictionary<LongGuid, ScriptableObjectCollection> collectionGuidLookup = new();
25+
2426
[SerializeField, HideInInspector]
2527
private bool autoSearchForCollections;
2628
public bool AutoSearchForCollections => autoSearchForCollections;
@@ -47,9 +49,10 @@ public void RegisterCollection(ScriptableObjectCollection targetCollection)
4749
{
4850
if (collections.Contains(targetCollection))
4951
return;
50-
52+
5153
collections.Add(targetCollection);
52-
54+
RebuildGuidLookup();
55+
5356
ObjectUtility.SetDirty(this);
5457
}
5558

@@ -60,7 +63,8 @@ public void UnregisterCollection(ScriptableObjectCollection targetCollection)
6063

6164
if (!collections.Remove(targetCollection))
6265
return;
63-
66+
67+
RebuildGuidLookup();
6468
ObjectUtility.SetDirty(this);
6569
}
6670

@@ -243,14 +247,34 @@ public ScriptableObjectCollection GetCollectionByGUID(string guid)
243247

244248
public ScriptableObjectCollection GetCollectionByGUID(LongGuid guid)
245249
{
250+
if (collectionGuidLookup.Count == 0 && collections.Count > 0)
251+
RebuildGuidLookup();
252+
253+
if (collectionGuidLookup.TryGetValue(guid, out ScriptableObjectCollection cached) && cached != null)
254+
return cached;
255+
246256
for (int i = 0; i < collections.Count; i++)
247257
{
248258
if (collections[i] != null && collections[i].GUID == guid)
259+
{
260+
collectionGuidLookup[guid] = collections[i];
249261
return collections[i];
262+
}
250263
}
251264

252265
return null;
253266
}
267+
268+
private void RebuildGuidLookup()
269+
{
270+
collectionGuidLookup.Clear();
271+
for (int i = 0; i < collections.Count; i++)
272+
{
273+
ScriptableObjectCollection collection = collections[i];
274+
if (collection != null && collection.GUID.IsValid())
275+
collectionGuidLookup[collection.GUID] = collection;
276+
}
277+
}
254278

255279
public bool TryGetCollectionOfType(Type type, out ScriptableObjectCollection resultCollection)
256280
{
@@ -377,6 +401,7 @@ public void ReloadCollections()
377401
{
378402
ValidateCollections();
379403
collections = foundCollections;
404+
RebuildGuidLookup();
380405
ObjectUtility.SetDirty(this);
381406
}
382407
#endif

0 commit comments

Comments
 (0)