Advertisement
Guest User

Untitled

a guest
Oct 24th, 2014
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.69 KB | None | 0 0
  1. public class Singleton<T> : BaseBehaviour where T : MonoBehaviour
  2.     {
  3.         protected static T _instance;
  4.  
  5.         /**
  6.        Returns the instance of this singleton.
  7.     */
  8.         public static T Instance
  9.         {
  10.             get
  11.             {
  12.                 if (_instance == null)
  13.                 {
  14.                     _instance = (T)FindObjectOfType(typeof(T));
  15.  
  16.                     if (_instance == null)
  17.                     {
  18.                         var o = new GameObject("#Singleton<" + typeof(T).Name + ">");
  19.                         _instance = o.AddComponent<T>();
  20.                     }
  21.                 }
  22.  
  23.                 return _instance;
  24.             }
  25.         }
  26.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement