Skip to content

Commit 0efd773

Browse files
committed
Update
1 parent 5d7829e commit 0efd773

10 files changed

+107
-27
lines changed

Runtime/EditorStorage.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#if UNITY_EDITOR
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
using UnityEditor;
5+
#if ODIN_INSPECTOR
6+
using Sirenix.Utilities.Editor;
7+
#endif
8+
9+
namespace ToolBox.Loader.Editor
10+
{
11+
public static class EditorStorage
12+
{
13+
public static List<T> GetAllAssetsOfType<T>() where T : Object
14+
{
15+
#if ODIN_INSPECTOR
16+
var assets = AssetUtilities.GetAllAssetsOfType<ScriptableObject>().ToArray();
17+
#else
18+
var paths = AssetDatabase.GetAllAssetPaths();
19+
var assets = new List<T>();
20+
21+
foreach (var path in paths)
22+
{
23+
var asset = AssetDatabase.LoadAssetAtPath<T>(path);
24+
25+
if (asset is T value)
26+
assets.Add(value);
27+
}
28+
29+
return assets;
30+
#endif
31+
}
32+
}
33+
}
34+
#endif

Runtime/EditorStorage.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace ToolBox.Loader
2+
{
3+
public interface IInitializableBeforeBuild
4+
{
5+
void Init();
6+
}
7+
}

Runtime/IInitializableBeforeBuild.cs.meta

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

Runtime/ILoadable.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
namespace ToolBox.Loader
2+
{
3+
public interface ILoadable { }
4+
}

Runtime/ILoadable.cs.meta

Lines changed: 11 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: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
using System.Collections.Generic;
22
using System.Linq;
3-
using UnityEngine;
4-
#if UNITY_EDITOR && ODIN_INSPECTOR
5-
using Sirenix.Utilities.Editor;
3+
#if UNITY_EDITOR
4+
using ToolBox.Loader.Editor;
65
#endif
6+
using UnityEngine;
77

88
namespace ToolBox.Loader
99
{
@@ -15,7 +15,7 @@ public static class Storage
1515
private static void Setup()
1616
{
1717
#if UNITY_EDITOR
18-
var assets = GetAssets();
18+
var assets = EditorStorage.GetAllAssetsOfType<ScriptableObject>().ToArray();
1919
#else
2020
var assets = Resources.LoadAll<ScriptableObject>("");
2121
#endif
@@ -28,7 +28,7 @@ public static T Get<T>() where T : ScriptableObject, ILoadable
2828
{
2929
#if UNITY_EDITOR
3030
if (!Application.isPlaying)
31-
_assets = GetAssets();
31+
_assets = EditorStorage.GetAllAssetsOfType<ScriptableObject>().ToArray();
3232
#endif
3333

3434
for (int i = 0; i < _assets.Length; i++)
@@ -42,25 +42,10 @@ public static IEnumerable<T> GetAll<T>() where T : ScriptableObject, ILoadable
4242
{
4343
#if UNITY_EDITOR
4444
if (!Application.isPlaying)
45-
_assets = GetAssets();
45+
_assets = EditorStorage.GetAllAssetsOfType<ScriptableObject>().ToArray();
4646
#endif
4747

4848
return _assets.Where(a => a is T).Cast<T>();
4949
}
50-
51-
#if UNITY_EDITOR
52-
private static ScriptableObject[] GetAssets()
53-
{
54-
#if ODIN_INSPECTOR
55-
var assets = AssetUtilities.GetAllAssetsOfType<ScriptableObject>().ToArray();
56-
#else
57-
var assets = Resources.FindObjectsOfTypeAll<ScriptableObject>();
58-
#endif
59-
60-
return assets;
61-
}
62-
#endif
6350
}
64-
65-
public interface ILoadable { }
6651
}

Runtime/StorageBuildProcessor.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
using UnityEditor.Build.Reporting;
66
using UnityEngine;
77

8-
namespace ToolBox.Loader
8+
namespace ToolBox.Loader.Editor
99
{
1010
public class StorageBuildProcessor : IPreprocessBuildWithReport, IPostprocessBuildWithReport
1111
{
@@ -18,14 +18,20 @@ public void OnPreprocessBuild(BuildReport report)
1818
if (!AssetDatabase.IsValidFolder("Assets/Resources"))
1919
AssetDatabase.CreateFolder("Assets", "Resources");
2020

21-
var loadables = Resources.FindObjectsOfTypeAll<ScriptableObject>().Where(x => x is ILoadable).ToArray();
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+
2228
_loadables = new ScriptableObject[loadables.Length];
2329

2430
for (int i = 0; i < loadables.Length; i++)
2531
{
2632
var loadable = loadables[i];
2733
var copy = Object.Instantiate(loadable);
28-
var path = $"Assets/Resources/{loadable.name}.asset";
34+
string path = $"Assets/Resources/{loadable.name}.asset";
2935

3036
AssetDatabase.CreateAsset(copy, path);
3137
_loadables[i] = copy;
Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
11
{
2-
"name": "com.intothedev.loader"
3-
}
2+
"name": "com.intothedev.loader",
3+
"rootNamespace": "",
4+
"references": [],
5+
"includePlatforms": [],
6+
"excludePlatforms": [],
7+
"allowUnsafeCode": false,
8+
"overrideReferences": false,
9+
"precompiledReferences": [],
10+
"autoReferenced": true,
11+
"defineConstraints": [],
12+
"versionDefines": [],
13+
"noEngineReferences": false
14+
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "com.intothedev.loader",
3-
"version": "1.3.0",
3+
"version": "1.4.0",
44
"displayName": "ScriptableObject Loader",
55
"description": "Load Scriptable Objects via code.",
66
"author": {

0 commit comments

Comments
 (0)