Advertisement
mvaganov

Singleton.cs

Feb 15th, 2018
320
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.49 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. // license: Copyfree, public domain. This is free code! Great artists, steal this code!
  6. // latest version at: https://pastebin.com/raw/JMme8A2X
  7. // works well with SceneCombiner: https://pastebin.com/raw/8cZ5yKSd
  8. namespace NS {
  9.     public class Singleton : MonoBehaviour {
  10.         [Tooltip("If another Singleton object with this onlyAllowOne is added to the scene, it will Destroy itself after starting.\nWorks well with SceneCombiner.")]
  11.         public string onlyAllowOne = "";
  12.         [Tooltip("Mark true to keep this object in the scene when changing to another scene.\nThis can be a tricky design, be careful!")]
  13.         public bool dontDestroyOnLoad = true;
  14.         void Start () {
  15.             bool alreadyHere = false;
  16.             GameObject[] objs = FindObjectsOfType<GameObject> ();
  17.             for (int i = 0; i < objs.Length && !alreadyHere; ++i) {
  18.                 Singleton[] singles = objs [i].GetComponents<Singleton> ();
  19.                 if (singles != null && singles.Length > 0) {
  20.                     for (int d = 0; d < singles.Length; ++d) {
  21.                         // if one of me already exists...
  22.                         if (singles [d] != this && ((onlyAllowOne != "" && singles [d].onlyAllowOne == onlyAllowOne)
  23.                                                 || (onlyAllowOne == "" && singles [d].gameObject.name == gameObject.name))) {
  24.                             alreadyHere = true;
  25.                             break;
  26.                         }
  27.                     }
  28.                 }
  29.             }
  30.             if (alreadyHere) {
  31.                 Debug.Log("\'"+onlyAllowOne+"\' Singleton already exists, removing \'"+gameObject+"\'");
  32.                 gameObject.SetActive (false);
  33.                 onlyAllowOne = null;
  34.                 Destroy (gameObject);
  35.             } else if(dontDestroyOnLoad) {
  36.                 DontDestroyOnLoad (transform.gameObject);
  37.             }
  38.         }
  39.  
  40.         private static Dictionary<System.Type, Component> instances = new Dictionary<System.Type, Component>();
  41.         public static T GetComponentInstance<T>() where T : Component {
  42.             Component instance = null;
  43.             if(!instances.TryGetValue(typeof(T), out instance)) {
  44.                 T[] searchInstance = GameObject.FindObjectsOfType<T> ();
  45.                 if (searchInstance != null) {
  46.                     if (searchInstance.Length > 1) {
  47.                         throw new System.Exception ("multiple instances of " + typeof(T).Name + " found, should be singleton");
  48.                     } else if (searchInstance.Length != 0) {
  49.                         instance = searchInstance [0];
  50.                     }
  51.                 }
  52.                 if(instance == null) {
  53.                     GameObject g = new GameObject();
  54.                     instance = g.AddComponent<T>();
  55.                     g.name = "<" + instance.GetType().Name + ">";
  56.                     instances [typeof(T)] = instance;
  57.                 }
  58.             }
  59.             return instance as T;
  60.         }
  61.     }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement