public static void Set(T newT, bool overwrite = false, System.Action <T> ifOverwriteDoThisOnOriginal = null, System.Action <T> ifNotOverwriteDoThisOnNewT = null)
        {
            lock (m_Lock)
            {
                if (newT == null)
                {
                    if (instance != null)
                    {
                        MonoBehaviour.Destroy(instance);
                    }
                    instance = null;
                    return;
                }

                Debug.Log($"Trying to set Singleton of type {newT.GetType().Name}...instance: " + newT);
                if (SingletonBehaviourLocator <T> .instance == null)
                {
                    SingletonBehaviourLocator <T> .instance = newT;
                }
                if (SingletonBehaviourLocator <T> .instance != newT)
                {
                    if (!overwrite)
                    {
                        ifNotOverwriteDoThisOnNewT?.Invoke(newT);
                        MonoBehaviour.DestroyImmediate(newT);
                    }
                    else
                    {
                        ifOverwriteDoThisOnOriginal?.Invoke(Instance);
                        MonoBehaviour.DestroyImmediate(Instance);
                        SingletonBehaviourLocator <T> .instance = newT;
                    }
                }

                try
                {
                    #if DDOL_EXT
                    DDOLRegistry.DontDestroyOnLoad(newT.gameObject);
                    #else
                    MonoBehaviour.DontDestroyOnLoad(newT);
                    #endif
                }
                catch (System.Exception e)
                {
                    Debug.Log("SBL:: Setting singleton of " + newT.GetType().Name + ", Exception: " + e);
                }
            }
        }
 public GameObjectPool(GameObject[] prefabs, int defaultPop = 5, Transform parent = null, bool dontDestroyOnLoad = true)
 {
     if (parent != null)
     {
         PoolGO = parent.gameObject;
     }
     TryInit();
     if (dontDestroyOnLoad)
     {
                         #if DDOL_EXT
         DDOLRegistry.DontDestroyOnLoad(PoolGO);
                         #else
         MonoBehaviour.DontDestroyOnLoad(PoolGO);
                         #endif
     }
     pooledPrefab = prefabs;
     poolList.AddRange(NewPooledObjects(defaultPop));
 }
Exemple #3
0
        public void Reload()
        {
            DontDestroyOnLoad(this);
            List <GameObject> t = new List <GameObject>(this.gameObject.scene.GetRootGameObjects());

                        #if DDOL_EXT
            t.Union(DDOLRegistry.GetDDOLs());
                        #endif
            foreach (var root in t)
            {
                if (!dontUnloadFilter.Match(root as GameObject))
                {
                    continue;
                }
                else
                {
                    Destroy(root);
                }
            }

            UnityEngine.SceneManagement.SceneManager.LoadScene(targetSceneName);
        }
 public GameObjectPool(T[] prefabs, int defaultPop = 5, Transform parent = null, bool dontDestroyOnLoad = true)
 {
     if (parent != null)
     {
         PoolGO = parent.gameObject;
     }
     TryInit();
     if (dontDestroyOnLoad)
     {
                         #if DDOL_EXT
         DDOLRegistry.DontDestroyOnLoad(PoolGO);
                         #else
         MonoBehaviour.DontDestroyOnLoad(PoolGO);
                         #endif
     }
     pooledPrefab = new GameObject[prefabs.Length];
     for (int i = 0; i < prefabs.Length; ++i)
     {
         pooledPrefab[i] = prefabs[i].gameObject;
     }
     poolList.AddRange(NewPooledObjects(defaultPop));
 }