Advertisement
Guest User

Untitled

a guest
May 22nd, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.94 KB | None | 0 0
  1. using System.Collections;
  2. using UnityEngine;
  3.  
  4. namespace MyGame {
  5.     public class Singleton<T> : MonoBehaviour where T : class {
  6.  
  7.         private static T instance = null;
  8.  
  9.         public static T Instance {
  10.             get {
  11.                 if (instance == null)
  12.                     instance = SingletonManager.Go.AddComponent(typeof(T)) as T;
  13.                 return instance;
  14.             }
  15.         }
  16.  
  17.         public static void Instantiate() {
  18.             instance = Instance;
  19.         }
  20.  
  21.         public Singleton() {
  22.             instance = this as T;
  23.         }
  24.     }
  25.  
  26.     public class SingletonManager {
  27.  
  28.         private static GameObject go = null;
  29.  
  30.         public static GameObject Go {
  31.             get {
  32.                 if (go == null) {
  33.                     go = new GameObject("Singleton");
  34.                     Object.DontDestroyOnLoad(go);
  35.                 }
  36.                 return go;
  37.             }
  38.         }
  39.     }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement