thieumao

Singleton

Oct 7th, 2015
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.68 KB | None | 0 0
  1. using System.ComponentModel;
  2. using UnityEngine;
  3. using System.Collections;
  4.  
  5. public class Singleton<T> : MonoBehaviour where T : MonoBehaviour
  6. {
  7.     private static T _instance;
  8.  
  9.     public static T Instance
  10.     {
  11.         get
  12.         {
  13.             if (_instance == null)
  14.             {
  15.                 _instance = (T)FindObjectOfType(typeof(T));
  16.                 if (_instance == null)
  17.                 {
  18.                     var singleton = new GameObject();
  19.                     _instance = singleton.AddComponent<T>();
  20.                     singleton.name =  typeof(T).ToString();
  21.                   ;
  22.                 }
  23.             }
  24.             return _instance;
  25.         }
  26.     }
  27. }
Advertisement
Add Comment
Please, Sign In to add comment