Last active
November 30, 2018 20:59
-
-
Save aomnes/8829de0613080abc41ada45cf437dccc to your computer and use it in GitHub Desktop.
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 : Singleton<T> | |
{ | |
protected static T instance; | |
/** | |
Returns the instance of this singleton. | |
*/ | |
public static T Instance | |
{ | |
get | |
{ | |
if (instance != null) return instance; | |
instance = (T) FindObjectOfType(typeof(T)); | |
if (instance == null) | |
{ | |
Debug.LogError(string.Format("An instance of {0} is needed in the scene, but there is none.", typeof(T))); | |
} | |
return instance; | |
} | |
} | |
public static bool IsInitialized | |
{ | |
get { return instance != null; } | |
} | |
protected virtual void OnDestroy() | |
{ | |
if (instance == this) | |
{ | |
instance = null; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment