-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathScriptableSingleton.cs
More file actions
65 lines (55 loc) · 1.55 KB
/
ScriptableSingleton.cs
File metadata and controls
65 lines (55 loc) · 1.55 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
using OlegHcp.Tools;
using UnityEngine;
namespace OlegHcp.SingleScripts
{
/// <summary>
/// Represents implementation of ScriptableObject singleton with lazy initialization.
/// </summary>
public abstract class ScriptableSingleton<T> : ScriptableObject where T : ScriptableSingleton<T>
{
private static T _instance;
/// <summary>
/// Static instance of ScriptableSingleton`1.
/// </summary>
public static T I
{
get
{
if (_instance == null)
{
if (!CreateInstanceAttribute.TryUse<T>())
CreateInstance<T>();
}
return _instance;
}
}
/// <summary>
/// Returns true if the instance is not null.
/// </summary>
public static bool Exists => _instance != null;
private void OnEnable()
{
if (_instance != null)
{
if (this != _instance)
DebugErrors.MultipleInstancesMessage<T>();
return;
}
Construct();
_instance = this as T;
}
private void OnDisable()
{
_instance = null;
Destruct();
}
/// <summary>
/// Used it instead of Awake.
/// </summary>
protected abstract void Construct();
/// <summary>
/// Used it instead of OnDestroy.
/// </summary>
protected abstract void Destruct();
}
}