Skip to content

Commit 9c364d3

Browse files
authored
Merge pull request #2 from IntoTheDev/v1.5.0
Big performance improvements
2 parents d0592cf + ed4e08f commit 9c364d3

File tree

6 files changed

+84
-55
lines changed

6 files changed

+84
-55
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: 44 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,43 @@
1+
using System;
12
using System.Collections.Generic;
3+
using System.Diagnostics;
24
using System.Linq;
35
#if UNITY_EDITOR
46
using ToolBox.Loader.Editor;
7+
using UnityEditor;
58
#endif
69
using UnityEngine;
710

811
namespace 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
}

Runtime/StorageBuildProcessor.cs

Lines changed: 5 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,16 @@
1-
#if UNITY_EDITOR
2-
using System.Linq;
3-
using UnityEditor;
1+
#if UNITY_EDITOR
42
using UnityEditor.Build;
53
using UnityEditor.Build.Reporting;
64
using UnityEngine;
75

86
namespace ToolBox.Loader.Editor
97
{
10-
public class StorageBuildProcessor : IPreprocessBuildWithReport, IPostprocessBuildWithReport
8+
public class StorageBuildProcessor : IPreprocessBuildWithReport
119
{
12-
private ScriptableObject[] _loadables = null;
13-
1410
public int callbackOrder => 0;
1511

16-
public void OnPreprocessBuild(BuildReport report)
17-
{
18-
if (!AssetDatabase.IsValidFolder("Assets/Resources"))
19-
AssetDatabase.CreateFolder("Assets", "Resources");
20-
21-
var assets = EditorStorage.GetAllAssetsOfType<ScriptableObject>();
22-
var loadables = assets.Where(x => x is ILoadable).ToArray();
23-
var initializables = assets.Where(x => x is IInitializableBeforeBuild).Cast<IInitializableBeforeBuild>();
24-
25-
foreach (var initializable in initializables)
26-
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();
51-
}
12+
public void OnPreprocessBuild(BuildReport report) =>
13+
Resources.Load<Storage>("ToolBoxStorage").LoadAssets();
5214
}
5315
}
54-
#endif
16+
#endif

0 commit comments

Comments
 (0)