1+ using System ;
12using System . Collections . Generic ;
3+ using System . Diagnostics ;
24using System . Linq ;
35#if UNITY_EDITOR
46using ToolBox . Loader . Editor ;
7+ using UnityEditor ;
58#endif
69using UnityEngine ;
710
811namespace ToolBox . Loader
912{
10- public static class Storage
13+ public class Storage : ScriptableObject
1114 {
12- private static ScriptableObject [ ] _assets = null ;
15+ [ SerializeField ] private ScriptableObject [ ] _assets = null ;
16+
17+ private static ILoadable [ ] _loadables = new ILoadable [ 0 ] ;
1318
1419 [ RuntimeInitializeOnLoadMethod ( RuntimeInitializeLoadType . SubsystemRegistration ) ]
1520 private static void Setup ( )
1621 {
1722#if UNITY_EDITOR
18- var assets = EditorStorage . GetAllAssetsOfType < ScriptableObject > ( ) . ToArray ( ) ;
23+ LoadAssetsInEditor ( ) ;
1924#else
20- var assets = Resources . LoadAll < ScriptableObject > ( "" ) ;
25+ _loadables = Resources . Load < Storage > ( "ToolBoxStorage" ) . _assets . Cast < ILoadable > ( ) . ToArray ( ) ;
2126#endif
22- _assets = assets . Where ( x => x is ILoadable ) . ToArray ( ) ;
27+ for ( int i = 0 ; i < _loadables . Length ; i ++ )
28+ _loadables [ i ] . Load ( ) ;
2329 }
2430
2531 public static T Get < T > ( ) where T : ScriptableObject , ILoadable
2632 {
2733#if UNITY_EDITOR
2834 if ( ! Application . isPlaying )
29- _assets = EditorStorage . GetAllAssetsOfType < ScriptableObject > ( ) . ToArray ( ) ;
35+ LoadAssetsInEditor ( ) ;
3036#endif
3137
32- for ( int i = 0 ; i < _assets . Length ; i ++ )
33- if ( _assets [ i ] is T asset )
34- return asset ;
38+ for ( int i = 0 ; i < _loadables . Length ; i ++ )
39+ if ( _loadables [ i ] is T loadable )
40+ return loadable ;
3541
3642 return null ;
3743 }
@@ -40,10 +46,37 @@ public static IEnumerable<T> GetAll<T>() where T : ScriptableObject, ILoadable
4046 {
4147#if UNITY_EDITOR
4248 if ( ! Application . isPlaying )
43- _assets = EditorStorage . GetAllAssetsOfType < ScriptableObject > ( ) . ToArray ( ) ;
49+ LoadAssetsInEditor ( ) ;
4450#endif
4551
46- return _assets . Where ( a => a is T ) . Cast < T > ( ) ;
52+ return _loadables . Where ( a => a is T ) . Cast < T > ( ) ;
53+ }
54+
55+ #if UNITY_EDITOR
56+ private static void LoadAssetsInEditor ( )
57+ {
58+ _loadables = EditorStorage .
59+ GetAllAssetsOfType < ScriptableObject > ( ) .
60+ Where ( x => x is ILoadable ) .
61+ Cast < ILoadable > ( ) .
62+ ToArray ( ) ;
63+ }
64+
65+ internal void LoadAssets ( )
66+ {
67+ // I don't know why but if you haven't selected this asset, array will be empty in build and after
68+ Selection . activeObject = this ;
69+
70+ var assets = EditorStorage . GetAllAssetsOfType < ScriptableObject > ( ) ;
71+ var loadables = assets . Where ( x => x is ILoadable ) . ToArray ( ) ;
72+ var initializables = assets . Where ( x => x is IInitializableBeforeBuild ) . Cast < IInitializableBeforeBuild > ( ) ;
73+
74+ _assets = new ScriptableObject [ loadables . Length ] ;
75+ Array . Copy ( loadables , _assets , loadables . Length ) ;
76+
77+ foreach ( var initializable in initializables )
78+ initializable . Init ( ) ;
4779 }
80+ #endif
4881 }
4982}
0 commit comments