Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using UnityEngine;
- namespace MyGame {
- public class Singleton<T> : MonoBehaviour where T : class {
- private static T instance = null;
- public static T Instance {
- get {
- if (instance == null)
- instance = SingletonManager.Go.AddComponent(typeof(T)) as T;
- return instance;
- }
- }
- public static void Instantiate() {
- instance = Instance;
- }
- public Singleton() {
- instance = this as T;
- }
- }
- public class SingletonManager {
- private static GameObject go = null;
- public static GameObject Go {
- get {
- if (go == null) {
- go = new GameObject("Singleton");
- Object.DontDestroyOnLoad(go);
- }
- return go;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement