-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGlobalScriptableObject.cs
45 lines (32 loc) · 1.16 KB
/
GlobalScriptableObject.cs
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
using UnityEditor;
using UnityEngine;
using static TBox.Logger;
namespace TBox
{
public class GlobalScriptableObject<T> : ScriptableObject where T : GlobalScriptableObject<T>
{
protected static T instance;
public static T Instance => GetReference();
protected static T GetReference()
{
if (instance)
return instance;
// Search in assets
var name = typeof(T).Name;
var ids = AssetDatabase.FindAssets($"t:{name}");
if (ids.Length > 0)
instance = AssetDatabase.LoadAssetAtPath(AssetDatabase.GUIDToAssetPath(ids[0]), typeof(T)) as T;
if (ids.Length > 1)
LogWarn($"Multiple {name} files found");
if (instance)
return instance;
// Create the asset
instance = CreateInstance<T>();
AssetDatabase.CreateAsset(instance, $"Assets/{name}.asset");
AssetDatabase.SetLabels(instance, new[] { $"{name}" });
AssetDatabase.SaveAssets();
LogWarn($"Created missing {name} file", "GSO");
return instance;
}
}
}