Last active
February 2, 2018 08:44
-
-
Save nicloay/b49a8e91f3e182f5e9fb8525e983ecf3 to your computer and use it in GitHub Desktop.
Unity3d singleton
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
public class Singleton<T> : MonoBehaviour where T : MonoBehaviour | |
{ | |
static T _instance; | |
static readonly object _lock = new object(); | |
public static bool Initialized() | |
{ | |
return _instance != null; | |
} | |
public static T Instance | |
{ | |
get | |
{ | |
if (applicationIsQuitting) { | |
Debug.LogWarning("[Singleton] Instance '"+ typeof(T) + | |
"' already destroyed on application quit." + | |
" Won't create again - returning null."); | |
return null; | |
} | |
lock(_lock) | |
{ | |
if (_instance == null) | |
{ | |
_instance = (T) FindObjectOfType(typeof(T)); | |
if ( 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) | |
{ | |
GameObject singleton = new GameObject(); | |
_instance = singleton.AddComponent<T>(); | |
singleton.name = "(singleton) "+ typeof(T).ToString(); | |
DontDestroyOnLoad(singleton); | |
Debug.Log("[Singleton] An instance of " + typeof(T) + | |
" is needed in the scene, so '" + singleton + | |
"' was created with DontDestroyOnLoad."); | |
} else { | |
Debug.Log("[Singleton] Using instance already created: " + | |
_instance.gameObject.name); | |
} | |
} | |
return _instance; | |
} | |
} | |
} | |
protected static bool applicationIsQuitting = false; | |
/// <summary> | |
/// When Unity quits, it destroys objects in a random order. | |
/// In principle, a Singleton is only destroyed when application quits. | |
/// If any script calls Instance after it have been destroyed, | |
/// it will create a buggy ghost object that will stay on the Editor scene | |
/// even after stopping playing the Application. Really bad! | |
/// So, this was made to be sure we're not creating that buggy ghost object. | |
/// </summary> | |
public void OnApplicationQuit () | |
{ | |
applicationIsQuitting = true; | |
} | |
public void Awake() | |
{ | |
if (_instance == null) | |
{ | |
_instance = this as T; | |
DontDestroyOnLoad(this.gameObject); | |
} | |
else | |
{ | |
if (this != _instance) | |
Destroy(this); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment