5
5
using UnityEngine ;
6
6
using UnityEngine . Scripting ;
7
7
#if UNITY_EDITOR
8
+ using System . Text ;
8
9
using UnityEditor ;
9
10
#endif
10
11
@@ -14,12 +15,14 @@ namespace BrunoMikoski.ScriptableObjectCollections
14
15
[ Preserve ]
15
16
public class CollectionsRegistry : ResourceScriptableObjectSingleton < CollectionsRegistry >
16
17
{
17
- [ SerializeField ]
18
+ private const string NON_AUTO_INITIALIZED_COLLECTIONS_KEY = "NON_AUTO_INITIALIZED_COLLECTIONS" ;
19
+
20
+ [ SerializeField ]
18
21
private List < ScriptableObjectCollection > collections = new List < ScriptableObjectCollection > ( ) ;
19
22
public IReadOnlyList < ScriptableObjectCollection > Collections => collections ;
20
23
21
- [ SerializeField ]
22
- private bool autoSearchForCollections = true ;
24
+ [ SerializeField , HideInInspector ]
25
+ private bool autoSearchForCollections ;
23
26
public bool AutoSearchForCollections => autoSearchForCollections ;
24
27
25
28
[ RuntimeInitializeOnLoadMethod ( RuntimeInitializeLoadType . BeforeSceneLoad ) ]
@@ -371,6 +374,7 @@ public void PreBuildProcess()
371
374
public void RemoveNonAutomaticallyInitializedCollections ( )
372
375
{
373
376
#if UNITY_EDITOR
377
+ StringBuilder removedAssetPaths = new StringBuilder ( ) ;
374
378
bool dirty = false ;
375
379
for ( int i = collections . Count - 1 ; i >= 0 ; i -- )
376
380
{
@@ -380,13 +384,43 @@ public void RemoveNonAutomaticallyInitializedCollections()
380
384
continue ;
381
385
382
386
collections . Remove ( collection ) ;
387
+ removedAssetPaths . Append ( $ "{ AssetDatabase . GetAssetPath ( collection ) } |") ;
388
+
383
389
dirty = true ;
384
390
}
385
391
386
392
if ( dirty )
387
393
{
394
+ EditorPrefs . SetString ( NON_AUTO_INITIALIZED_COLLECTIONS_KEY , removedAssetPaths . ToString ( ) ) ;
388
395
ObjectUtility . SetDirty ( this ) ;
389
396
}
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 ) ;
390
424
#endif
391
425
}
392
426
@@ -453,5 +487,20 @@ public void SetAutoSearchForCollections(bool isOn)
453
487
autoSearchForCollections = isOn ;
454
488
ObjectUtility . SetDirty ( this ) ;
455
489
}
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
+ }
456
505
}
457
506
}
0 commit comments