Created
February 10, 2017 18:34
-
-
Save ahokinson/2df9fc4585d3c2e71ab82b876dc1f001 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 SingletonBehaviour<T> : MonoBehaviour where T : MonoBehaviour | |
{ | |
protected static T s_instance = null; | |
public static T Instance | |
{ | |
get | |
{ | |
if (s_instance == null) | |
{ | |
s_instance = FindObjectOfType<T>(); | |
} | |
return s_instance; | |
} | |
} | |
public virtual void Awake() | |
{ | |
if (s_instance == null) | |
{ | |
s_instance = this as T; | |
} | |
} | |
public virtual void OnDestroy() | |
{ | |
if (s_instance == this) | |
{ | |
s_instance = null; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment