daily pastebin goal
6%
SHARE
TWEET

Singleton.cs

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