Skip to content

Instantly share code, notes, and snippets.

@ahokinson
Created February 10, 2017 18:34
Show Gist options
  • Save ahokinson/2df9fc4585d3c2e71ab82b876dc1f001 to your computer and use it in GitHub Desktop.
Save ahokinson/2df9fc4585d3c2e71ab82b876dc1f001 to your computer and use it in GitHub Desktop.
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