Advertisement
Guest User

New GenericSingleton

a guest
Nov 25th, 2013
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.96 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class Singleton<T> : MonoBehaviour where T : Component {
  5.     [SerializeField]
  6.     bool _persistent = false;
  7.    
  8.     private static T _instance;
  9.     public static bool IsInstantianted { get { return _instance != null; } }
  10.  
  11.     public static T Instance {
  12.         get {
  13.             if (_instance == null) {
  14.                 _instance = (T)FindObjectOfType(typeof(T));
  15.                 if (_instance == null) {
  16.                     Debug.Log ("Instance: _Loading");
  17.                     Application.LoadLevelAdditive("_Loading");
  18.                     _instance = (T)FindObjectOfType(typeof(T));
  19.                 }
  20.             }
  21.             return _instance;
  22.         }
  23.     }
  24.    
  25.     public virtual void Awake() {
  26.         if (_persistent)
  27.             DontDestroyOnLoad(this);
  28.         if (IsInstantianted) {
  29.             Debug.LogWarning("Only one " + typeof(T) + " is allowed, destroying " + gameObject.name + ".");
  30.             Destroy(gameObject);
  31.         } else {
  32.             Debug.Log ("Awake: _Loading");
  33.             Application.LoadLevelAdditive("_Loading");
  34.             _instance = (T)FindObjectOfType(typeof(T));
  35.         }
  36.     }
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement