Пример #1
0
        protected override void OnInit()
        {
            base.OnInit();

            st.PoolMainTransform = new GameObject("UnitPool").transform;
            st.PoolTransforms    = new Dictionary <int, Transform>();
            st.PrefabInfos       = new Dictionary <int, PrefabInfo>();
            st.PoolGO            = new Dictionary <int, Queue <GameObject> >();
            st.UnitGOPrefabInfo  = new Dictionary <GameObject, PrefabInfo>();

            foreach (var prefabGO in PResourcesExt.LoadAllPrefabs <GameObject>())
            {
                AddPrefabInfo(prefabGO);
            }

            StartCoroutine(OnInitFillPool());
            StartCoroutine(CheckForLeaks());
        }
Пример #2
0
        private static T CreateInstance()
        {
            var sceneInstances = FindObjectsOfType <T>();

            if (sceneInstances.IsNotNullOrEmpty())
            {
                Debug.LogErrorFormat(
                    "Unexpected instances of {0} singleton: {1}",
                    typeof(T),
                    string.Join(", ", sceneInstances.Select(x => x.name))
                    );
                return(sceneInstances[0]);
            }

            GameObject instGO;
            T          instT;
            var        prefabComponents = PResourcesExt.LoadAllPrefabs <T>();

            if (prefabComponents.IsNullOrEmpty())
            {
                instGO = new GameObject($"[Singleton] {typeof(T)}");
                instT  = instGO.AddComponent <T>();
            }
            else
            {
                if (prefabComponents.Length > 1)
                {
                    Debug.LogErrorFormat(
                        "Unexpected instances of {0} singleton: {1}",
                        typeof(T),
                        string.Join(", ", prefabComponents.Select(x => x.name))
                        );
                }

                instGO = Instantiate(prefabComponents[0].gameObject);
                instT  = instGO.GetComponent <T>();
            }

            DontDestroyOnLoad(instGO);
            instT.OnInit();

            return(instT);
        }