Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Singleton<T> : MonoBehaviour where T : MonoBehaviour
- {
- public static bool isApplicationQuitting;
- private static T instance;
- private static System.Object lockObj = new System.Object();
- public static T Instance
- {
- get
- {
- if (isApplicationQuitting)
- return null;
- lock (lockObj)
- {
- if (instance == null)
- {
- instance = FindObjectOfType<T>();
- if (instance == null)
- {
- var singleton = new GameObject("[SINGLETON] " + typeof(T));
- instance = singleton.AddComponent<T>();
- DontDestroyOnLoad(singleton);
- }
- }
- return instance;
- }
- }
- }
- public virtual void OnDestroy()
- {
- isApplicationQuitting = true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement