forked from akbiggs/QuickUnityTools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSceneSingleton.cs
More file actions
37 lines (31 loc) · 1.3 KB
/
Copy pathSceneSingleton.cs
File metadata and controls
37 lines (31 loc) · 1.3 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
using UnityEngine;
using System.Collections;
/// <summary>
/// Represents a single-instance object that is placed in scenes.
/// This will not create the singleton, it just provides quick access to the Singleton.
///
/// I think this class is still kind of buggy in some situations, I'll fix it later.
/// </summary>
public class SceneSingleton<T> : MonoBehaviour where T : MonoBehaviour {
private static T _instance;
public static T instance {
get {
if (_instance == null) {
_instance = (T)GameObject.FindObjectOfType(typeof(T));
if (GameObject.FindObjectsOfType(typeof(T)).Length > 1) {
Debug.LogError("[Singleton] Something went really wrong - there should never be more than 1 singleton! Reopening the scene might fix it.");
return _instance;
}
if (_instance == null) {
Debug.LogWarning("[Singleton] An instance of " + typeof(T) + " is needed in the scene, but none exists!");
} else {
Debug.Log("[Singleton] Using instance already created: " + _instance.gameObject.name);
}
}
return _instance;
}
}
protected virtual void OnDestroy() {
_instance = null;
}
}