Skip to content

Commit 01273cd

Browse files
committed
fix: more tweaks for the access of the values
1 parent c28adc3 commit 01273cd

2 files changed

Lines changed: 28 additions & 11 deletions

File tree

CHANGELOG.MD

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111
- Changed the Refresh of the collections to only find items under the same folder.
1212
- Fixed a lot of scripts to work properly with multiple collections of the same type
1313
- Added some logs when `CollectionItems` are added/removed from collections to help the visibility
14-
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
1515

1616
## [1.5.4]
1717
### Changed

Scripts/Runtime/Core/ScriptableObjectCollection.cs

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ private void SyncGUID()
5252

5353
guid = Guid.NewGuid().ToString();
5454
#if UNITY_EDITOR
55-
guid = UnityEditor.AssetDatabase.AssetPathToGUID(UnityEditor.AssetDatabase.GetAssetPath(this));
55+
guid = AssetDatabase.AssetPathToGUID(UnityEditor.AssetDatabase.GetAssetPath(this));
5656
ObjectUtility.SetDirty(this);
5757
#endif
5858
}
@@ -124,6 +124,7 @@ public bool Add(ScriptableObjectCollectionItem item)
124124

125125
item.SetCollection(this);
126126
ObjectUtility.SetDirty(this);
127+
ClearCachedValues();
127128
return true;
128129
}
129130

@@ -135,7 +136,7 @@ public ScriptableObjectCollectionItem AddNew(Type collectionType, string assetNa
135136
throw new NotSupportedException();
136137

137138
ScriptableObjectCollectionItem item = (ScriptableObjectCollectionItem)CreateInstance(collectionType);
138-
string assetPath = Path.GetDirectoryName(UnityEditor.AssetDatabase.GetAssetPath(this));
139+
string assetPath = Path.GetDirectoryName(AssetDatabase.GetAssetPath(this));
139140
string parentFolderPath = Path.Combine(assetPath, "Items");
140141
AssetDatabaseUtils.CreatePathIfDontExist(parentFolderPath);
141142

@@ -164,7 +165,7 @@ public ScriptableObjectCollectionItem AddNew(Type collectionType, string assetNa
164165
string newFileName = Path.Combine(parentFolderPath, item.name + ".asset");
165166

166167
this.Add(item);
167-
UnityEditor.AssetDatabase.CreateAsset(item, newFileName);
168+
AssetDatabase.CreateAsset(item, newFileName);
168169
ObjectUtility.SetDirty(this);
169170
return item;
170171
}
@@ -373,25 +374,31 @@ internal void PrepareForEditorMode()
373374
ObjectUtility.SetDirty(this);
374375
}
375376

377+
protected virtual void ClearCachedValues()
378+
{
379+
}
376380
}
377381

378382
public class ScriptableObjectCollection<ObjectType> : ScriptableObjectCollection, IList<ObjectType>
379383
where ObjectType : ScriptableObjectCollectionItem
380384
{
381-
private static ScriptableObjectCollection<ObjectType> values;
382-
public static ScriptableObjectCollection<ObjectType> Values => values;
385+
protected static List<ObjectType> cachedValues;
386+
public static List<ObjectType> Values
387+
{
388+
get
389+
{
390+
if (cachedValues == null)
391+
cachedValues = CollectionsRegistry.Instance.GetAllCollectionItemsOfType<ObjectType>();
392+
return cachedValues;
393+
}
394+
}
383395

384396
public new ObjectType this[int index]
385397
{
386398
get => (ObjectType)base[index];
387399
set => base[index] = value;
388400
}
389401

390-
private void OnEnable()
391-
{
392-
values = this;
393-
}
394-
395402
public new IEnumerator<ObjectType> GetEnumerator()
396403
{
397404
using (IEnumerator<ScriptableObjectCollectionItem> itemEnum = base.GetEnumerator())
@@ -472,11 +479,13 @@ public ObjectType GetItemByGUID(string targetGUID)
472479
public void Add(ObjectType item)
473480
{
474481
base.Add(item);
482+
ClearCachedValues();
475483
}
476484

477485
public ObjectType Add(Type itemType = null)
478486
{
479487
ObjectType item = base.Add(itemType) as ObjectType;
488+
ClearCachedValues();
480489
return item;
481490
}
482491

@@ -498,11 +507,13 @@ public int IndexOf(ObjectType item)
498507
public void Insert(int index, ObjectType item)
499508
{
500509
base.Insert(index, item);
510+
ClearCachedValues();
501511
}
502512

503513
public bool Remove(ObjectType item)
504514
{
505515
bool remove = base.Remove(item);
516+
ClearCachedValues();
506517
return remove;
507518
}
508519

@@ -521,5 +532,11 @@ IEnumerator IEnumerable.GetEnumerator()
521532
{
522533
return base.GetEnumerator();
523534
}
535+
536+
protected override void ClearCachedValues()
537+
{
538+
cachedValues = null;
539+
}
540+
524541
}
525542
}

0 commit comments

Comments
 (0)