11using System ;
22using System . Collections . Generic ;
3+ using System . Diagnostics ;
34using System . Linq ;
45#if UNITY_EDITOR
56using ToolBox . Loader . Editor ;
7+ using UnityEditor ;
68#endif
79using UnityEngine ;
810
911namespace ToolBox . Loader
1012{
1113 public class Storage : ScriptableObject
1214 {
13- [ SerializeField ] private ScriptableObject [ ] _assets ;
15+ [ SerializeField ] private ScriptableObject [ ] _assets = null ;
1416
1517 private static ILoadable [ ] _loadables = new ILoadable [ 0 ] ;
1618
@@ -26,14 +28,6 @@ private static void Setup()
2628 _loadables [ i ] . Load ( ) ;
2729 }
2830
29- #if UNITY_EDITOR
30- internal void SetAssets ( ScriptableObject [ ] loadables )
31- {
32- _assets = new ScriptableObject [ loadables . Length ] ;
33- Array . Copy ( loadables , _assets , loadables . Length ) ;
34- }
35- #endif
36-
3731 public static T Get < T > ( ) where T : ScriptableObject , ILoadable
3832 {
3933#if UNITY_EDITOR
@@ -67,6 +61,22 @@ private static void LoadAssetsInEditor()
6761 Cast < ILoadable > ( ) .
6862 ToArray ( ) ;
6963 }
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 ( ) ;
79+ }
7080#endif
7181 }
7282}
0 commit comments