-
Notifications
You must be signed in to change notification settings - Fork 2
/
Singleton.cs
57 lines (52 loc) · 1.45 KB
/
Singleton.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
46
47
48
49
50
51
52
53
54
55
56
57
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.
using UnityEngine;
namespace HoloToolkit.Unity
{
/// <summary>
/// Singleton behaviour class, used for components that should only have one instance
/// </summary>
/// <typeparam name="T"></typeparam>
public class Singleton<T> : MonoBehaviour where T : Singleton<T>
{
private static T instance;
public static T Instance
{
get
{
return instance;
}
}
/// <summary>
/// Returns whether the instance has been initialized or not.
/// </summary>
public static bool IsInitialized
{
get
{
return instance != null;
}
}
/// <summary>
/// Base awake method that sets the singleton's unique instance.
/// </summary>
protected virtual void Awake()
{
if (instance != null)
{
Debug.LogErrorFormat("Trying to instantiate a second instance of singleton class {0}", GetType().Name);
}
else
{
instance = (T) this;
}
}
protected virtual void OnDestroy()
{
if (instance == this)
{
instance = null;
}
}
}
}