Skip to content

Commit 51f88c5

Browse files
committed
Performance improvements
1 parent d0592cf commit 51f88c5

File tree

6 files changed

+72
-42
lines changed

6 files changed

+72
-42
lines changed

Runtime/ILoadable.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
namespace ToolBox.Loader
22
{
3-
public interface ILoadable { }
3+
public interface ILoadable
4+
{
5+
void Load();
6+
}
47
}

Runtime/Resources.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
%YAML 1.1
2+
%TAG !u! tag:unity3d.com,2011:
3+
--- !u!114 &11400000
4+
MonoBehaviour:
5+
m_ObjectHideFlags: 0
6+
m_CorrespondingSourceObject: {fileID: 0}
7+
m_PrefabInstance: {fileID: 0}
8+
m_PrefabAsset: {fileID: 0}
9+
m_GameObject: {fileID: 0}
10+
m_Enabled: 1
11+
m_EditorHideFlags: 0
12+
m_Script: {fileID: 11500000, guid: 6b5c09a589fc4a342af3d68e42917775, type: 3}
13+
m_Name: ToolBoxStorage
14+
m_EditorClassIdentifier:
15+
_assets: []

Runtime/Resources/ToolBoxStorage.asset.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Runtime/Storage.cs

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using System.Collections.Generic;
23
using System.Linq;
34
#if UNITY_EDITOR
@@ -7,31 +8,42 @@
78

89
namespace ToolBox.Loader
910
{
10-
public static class Storage
11+
public class Storage : ScriptableObject
1112
{
12-
private static ScriptableObject[] _assets = null;
13+
[SerializeField, HideInInspector] private ScriptableObject[] _assets = null;
14+
15+
private static ILoadable[] _loadables = new ILoadable[0];
1316

1417
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
1518
private static void Setup()
1619
{
1720
#if UNITY_EDITOR
18-
var assets = EditorStorage.GetAllAssetsOfType<ScriptableObject>().ToArray();
21+
LoadAssetsInEditor();
1922
#else
20-
var assets = Resources.LoadAll<ScriptableObject>("");
23+
_loadables = Resources.Load<Storage>("ToolBoxStorage")._assets.Cast<ILoadable>().ToArray();
2124
#endif
22-
_assets = assets.Where(x => x is ILoadable).ToArray();
25+
for (int i = 0; i < _loadables.Length; i++)
26+
_loadables[i].Load();
27+
}
28+
29+
#if UNITY_EDITOR
30+
internal void SetAssets(ScriptableObject[] loadables)
31+
{
32+
_assets = new ScriptableObject[loadables.Length];
33+
Array.Copy(loadables, _assets, loadables.Length);
2334
}
35+
#endif
2436

2537
public static T Get<T>() where T : ScriptableObject, ILoadable
2638
{
2739
#if UNITY_EDITOR
2840
if (!Application.isPlaying)
29-
_assets = EditorStorage.GetAllAssetsOfType<ScriptableObject>().ToArray();
41+
LoadAssetsInEditor();
3042
#endif
3143

32-
for (int i = 0; i < _assets.Length; i++)
33-
if (_assets[i] is T asset)
34-
return asset;
44+
for (int i = 0; i < _loadables.Length; i++)
45+
if (_loadables[i] is T loadable)
46+
return loadable;
3547

3648
return null;
3749
}
@@ -40,10 +52,21 @@ public static IEnumerable<T> GetAll<T>() where T : ScriptableObject, ILoadable
4052
{
4153
#if UNITY_EDITOR
4254
if (!Application.isPlaying)
43-
_assets = EditorStorage.GetAllAssetsOfType<ScriptableObject>().ToArray();
55+
LoadAssetsInEditor();
4456
#endif
4557

46-
return _assets.Where(a => a is T).Cast<T>();
58+
return _loadables.Where(a => a is T).Cast<T>();
4759
}
60+
61+
#if UNITY_EDITOR
62+
private static void LoadAssetsInEditor()
63+
{
64+
_loadables = EditorStorage.
65+
GetAllAssetsOfType<ScriptableObject>().
66+
Where(x => x is ILoadable).
67+
Cast<ILoadable>().
68+
ToArray();
69+
}
70+
#endif
4871
}
4972
}

Runtime/StorageBuildProcessor.cs

Lines changed: 3 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -7,47 +7,20 @@
77

88
namespace ToolBox.Loader.Editor
99
{
10-
public class StorageBuildProcessor : IPreprocessBuildWithReport, IPostprocessBuildWithReport
10+
public class StorageBuildProcessor : IPreprocessBuildWithReport
1111
{
12-
private ScriptableObject[] _loadables = null;
13-
1412
public int callbackOrder => 0;
1513

1614
public void OnPreprocessBuild(BuildReport report)
1715
{
18-
if (!AssetDatabase.IsValidFolder("Assets/Resources"))
19-
AssetDatabase.CreateFolder("Assets", "Resources");
20-
2116
var assets = EditorStorage.GetAllAssetsOfType<ScriptableObject>();
2217
var loadables = assets.Where(x => x is ILoadable).ToArray();
2318
var initializables = assets.Where(x => x is IInitializableBeforeBuild).Cast<IInitializableBeforeBuild>();
2419

20+
assets.Where(x => x is Storage).Cast<Storage>().First().SetAssets(loadables);
21+
2522
foreach (var initializable in initializables)
2623
initializable.Init();
27-
28-
_loadables = new ScriptableObject[loadables.Length];
29-
30-
for (int i = 0; i < loadables.Length; i++)
31-
{
32-
var loadable = loadables[i];
33-
var copy = Object.Instantiate(loadable);
34-
string path = $"Assets/Resources/{loadable.name}.asset";
35-
36-
AssetDatabase.CreateAsset(copy, path);
37-
_loadables[i] = copy;
38-
}
39-
40-
AssetDatabase.SaveAssets();
41-
AssetDatabase.Refresh();
42-
}
43-
44-
public void OnPostprocessBuild(BuildReport report)
45-
{
46-
foreach (var loadable in _loadables)
47-
Object.DestroyImmediate(loadable, true);
48-
49-
AssetDatabase.SaveAssets();
50-
AssetDatabase.Refresh();
5124
}
5225
}
5326
}

0 commit comments

Comments
 (0)