-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathStorage.cs
More file actions
79 lines (66 loc) · 1.8 KB
/
Copy pathStorage.cs
File metadata and controls
79 lines (66 loc) · 1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
using System;
using System.Collections.Generic;
using System.Linq;
#if UNITY_EDITOR
using ToolBox.Loader.Editor;
using UnityEditor;
#endif
using UnityEngine;
namespace ToolBox.Loader
{
public class Storage : ScriptableObject
{
[SerializeField] private ScriptableObject[] _assets = null;
private static ILoadable[] _loadables = new ILoadable[0];
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
private static void Setup()
{
#if UNITY_EDITOR
LoadAssetsInEditor();
#else
_loadables = Resources.Load<Storage>("ToolBoxStorage")._assets.Cast<ILoadable>().ToArray();
#endif
for (int i = 0; i < _loadables.Length; i++)
_loadables[i].Load();
}
public static T Get<T>() where T : ScriptableObject, ILoadable
{
#if UNITY_EDITOR
if (!Application.isPlaying)
LoadAssetsInEditor();
#endif
for (int i = 0; i < _loadables.Length; i++)
if (_loadables[i] is T loadable)
return loadable;
return null;
}
public static IEnumerable<T> GetAll<T>() where T : ScriptableObject, ILoadable
{
#if UNITY_EDITOR
if (!Application.isPlaying)
LoadAssetsInEditor();
#endif
return _loadables.Where(a => a is T).Cast<T>();
}
#if UNITY_EDITOR
private static void LoadAssetsInEditor()
{
_loadables = EditorStorage
.GetAllAssetsOfType<ScriptableObject>()
.OfType<ILoadable>()
.ToArray();
}
internal void LoadAssets()
{
var assets = EditorStorage.GetAllAssetsOfType<ScriptableObject>();
var loadables = assets.OfType<ILoadable>().ToArray();
var initializables = assets.OfType<IInitializableBeforeBuild>();
_assets = new ScriptableObject[loadables.Length];
Array.Copy(loadables, _assets, loadables.Length);
foreach (var initializable in initializables)
initializable.Init();
EditorUtility.SetDirty(this);
}
#endif
}
}