Skip to content

Commit 15192c3

Browse files
authored
Update Storage.cs
1 parent 87c0477 commit 15192c3

File tree

1 file changed

+19
-9
lines changed

1 file changed

+19
-9
lines changed

Runtime/Storage.cs

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Diagnostics;
34
using System.Linq;
45
#if UNITY_EDITOR
56
using ToolBox.Loader.Editor;
7+
using UnityEditor;
68
#endif
79
using UnityEngine;
810

911
namespace 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

Comments
 (0)