Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- public class GlobalManager<T> : MonoBehaviour {
- [SerializeField]
- bool _persistent = false;
- private static T _instance;
- public static bool IsInstantianted { get { return _instance != null; } }
- protected virtual void Init() {}
- public static T Instance {
- get {
- if (_instance != null)
- return _instance;
- else {
- string errorMessage = typeof(T).Name + " is not attached to a GameObject or inactive.";
- Debug.LogError(errorMessage);
- throw new System.NullReferenceException(errorMessage);
- }
- }
- }
- void Awake() {
- if (Instantiation())
- Init();
- }
- bool Instantiation() {
- if (IsInstantianted) {
- Debug.LogWarning("Only one " + typeof(T) + " is allowed, destroying " + gameObject.name);
- Destroy(gameObject);
- return false;
- }
- _instance = (T) (object) this;
- if (_persistent)
- DontDestroyOnLoad(this);
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement