55using UnityEngine ;
66using UnityEngine . Scripting ;
77#if UNITY_EDITOR
8+ using System . Text ;
89using UnityEditor ;
910#endif
1011
@@ -14,12 +15,14 @@ namespace BrunoMikoski.ScriptableObjectCollections
1415 [ Preserve ]
1516 public class CollectionsRegistry : ResourceScriptableObjectSingleton < CollectionsRegistry >
1617 {
17- [ SerializeField ]
18+ private const string NON_AUTO_INITIALIZED_COLLECTIONS_KEY = "NON_AUTO_INITIALIZED_COLLECTIONS" ;
19+
20+ [ SerializeField ]
1821 private List < ScriptableObjectCollection > collections = new List < ScriptableObjectCollection > ( ) ;
1922 public IReadOnlyList < ScriptableObjectCollection > Collections => collections ;
2023
21- [ SerializeField ]
22- private bool autoSearchForCollections = true ;
24+ [ SerializeField , HideInInspector ]
25+ private bool autoSearchForCollections ;
2326 public bool AutoSearchForCollections => autoSearchForCollections ;
2427
2528 [ RuntimeInitializeOnLoadMethod ( RuntimeInitializeLoadType . BeforeSceneLoad ) ]
@@ -371,6 +374,7 @@ public void PreBuildProcess()
371374 public void RemoveNonAutomaticallyInitializedCollections ( )
372375 {
373376#if UNITY_EDITOR
377+ StringBuilder removedAssetPaths = new StringBuilder ( ) ;
374378 bool dirty = false ;
375379 for ( int i = collections . Count - 1 ; i >= 0 ; i -- )
376380 {
@@ -380,13 +384,43 @@ public void RemoveNonAutomaticallyInitializedCollections()
380384 continue ;
381385
382386 collections . Remove ( collection ) ;
387+ removedAssetPaths . Append ( $ "{ AssetDatabase . GetAssetPath ( collection ) } |") ;
388+
383389 dirty = true ;
384390 }
385391
386392 if ( dirty )
387393 {
394+ EditorPrefs . SetString ( NON_AUTO_INITIALIZED_COLLECTIONS_KEY , removedAssetPaths . ToString ( ) ) ;
388395 ObjectUtility . SetDirty ( this ) ;
389396 }
397+ else
398+ {
399+ EditorPrefs . DeleteKey ( NON_AUTO_INITIALIZED_COLLECTIONS_KEY ) ;
400+ }
401+ #endif
402+ }
403+
404+ public void ReloadUnloadedCollectionsIfNeeded ( )
405+ {
406+ #if UNITY_EDITOR
407+ string removedAssetPaths = EditorPrefs . GetString ( NON_AUTO_INITIALIZED_COLLECTIONS_KEY , string . Empty ) ;
408+ if ( string . IsNullOrEmpty ( removedAssetPaths ) )
409+ return ;
410+
411+ string [ ] paths = removedAssetPaths . Split ( '|' , StringSplitOptions . RemoveEmptyEntries ) ;
412+ for ( int i = 0 ; i < paths . Length ; i ++ )
413+ {
414+ string path = paths [ i ] ;
415+ ScriptableObjectCollection collection = AssetDatabase . LoadAssetAtPath < ScriptableObjectCollection > ( path ) ;
416+ if ( collection == null )
417+ continue ;
418+
419+ collections . Add ( collection ) ;
420+ }
421+
422+ EditorPrefs . DeleteKey ( NON_AUTO_INITIALIZED_COLLECTIONS_KEY ) ;
423+ ObjectUtility . SetDirty ( this ) ;
390424#endif
391425 }
392426
@@ -453,5 +487,20 @@ public void SetAutoSearchForCollections(bool isOn)
453487 autoSearchForCollections = isOn ;
454488 ObjectUtility . SetDirty ( this ) ;
455489 }
490+
491+ public void UpdateAutoSearchForCollections ( )
492+ {
493+ for ( int i = 0 ; i < Collections . Count ; i ++ )
494+ {
495+ ScriptableObjectCollection collection = Collections [ i ] ;
496+ if ( ! collection . AutomaticallyLoaded )
497+ {
498+ SetAutoSearchForCollections ( true ) ;
499+ return ;
500+ }
501+ }
502+
503+ SetAutoSearchForCollections ( false ) ;
504+ }
456505 }
457506}
0 commit comments