-
Notifications
You must be signed in to change notification settings - Fork 189
Expand file tree
/
Copy pathSingletonScriptableObject.cs
More file actions
50 lines (47 loc) · 1.84 KB
/
SingletonScriptableObject.cs
File metadata and controls
50 lines (47 loc) · 1.84 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
using System.Linq;
using UnityEditor;
using UnityEngine;
namespace UnityQuickSheet
{
/// <summary>
/// Abstract class for making reload-proof singletons out of ScriptableObjects
/// Returns the asset created on editor, null if there is none
/// Based on https://www.youtube.com/watch?v=VBA1QCoEAX4
///
/// See Also:
/// blog page: http://baraujo.net/unity3d-making-singletons-from-scriptableobjects-automatically/
/// gist page: https://gist.github.com/baraujo/07bb162a1f916595cad1a2d1fee5e72d
/// </summary>
/// <typeparam name="T">Type of the singleton</typeparam>
public abstract class SingletonScriptableObject<T> : ScriptableObject where T : ScriptableObject
{
static T _instance = null;
public static T Instance
{
get
{
if (!_instance)
{
_instance = Resources.FindObjectsOfTypeAll<T>().FirstOrDefault();
if (!_instance)
{
var assets = Util.FindAssetsByType<T>();
if (assets.Length > 0)
{
if (assets.Length > 1)
Debug.LogWarningFormat("Multiple {0} assets are found.", typeof(T));
_instance = assets[0];
Debug.LogFormat("Using {0}.", AssetDatabase.GetAssetPath(_instance));
}
// no found any setting file.
else
{
Debug.LogWarning("No instance of " + typeof(T).Name + " is loaded. Please create a " + typeof(T).Name + " asset file.");
}
}
}
return _instance;
}
}
}
}