Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- public class Singleton<T> : MonoBehaviour where T : Component {
- [SerializeField]
- bool _persistent = false;
- private static T _instance;
- public static bool IsInstantianted { get { return _instance != null; } }
- public static T Instance {
- get {
- if (_instance == null) {
- _instance = (T)FindObjectOfType(typeof(T));
- if (_instance == null) {
- Debug.Log ("Instance: _Loading");
- Application.LoadLevelAdditive("_Loading");
- _instance = (T)FindObjectOfType(typeof(T));
- }
- }
- return _instance;
- }
- }
- public virtual void Awake() {
- if (_persistent)
- DontDestroyOnLoad(this);
- if (IsInstantianted) {
- Debug.LogWarning("Only one " + typeof(T) + " is allowed, destroying " + gameObject.name + ".");
- Destroy(gameObject);
- } else {
- Debug.Log ("Awake: _Loading");
- Application.LoadLevelAdditive("_Loading");
- _instance = (T)FindObjectOfType(typeof(T));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement