Skip to content

Instantly share code, notes, and snippets.

@j4rv
Created October 11, 2018 19:11
Show Gist options
  • Save j4rv/945b50531be067a967c073e22ca9ba47 to your computer and use it in GitHub Desktop.
Save j4rv/945b50531be067a967c073e22ca9ba47 to your computer and use it in GitHub Desktop.
using UnityEngine;
public class LazyngletonBehaviour<T> : MonoBehaviour where T : LazyngletonBehaviour<T> {
private static T _instance;
public static T Instance {
get{
if(_instance == null){
GameObject newInstance = new GameObject();
_instance = newInstance.AddComponent<T>();
newInstance.name = _instance.GetType().Name;
_instance.OnFirstCall();
}
return _instance;
}
}
protected virtual void OnFirstCall () {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment