Пример #1
0
        private void AddPrefabInfo(GameObject prefabGO)
        {
            PAssert.IsNotNull(prefabGO, nameof(prefabGO));

            var        poolInfoComponent = prefabGO.GetComponent <PBaseUnitPoolInfo>();
            PrefabInfo prefabInfo;

            if (poolInfoComponent != null)
            {
                prefabInfo = new PrefabInfo {
                    PrefabGO                  = prefabGO,
                    InitialInstances          = poolInfoComponent.InitialInstances,
                    MaxInstances              = poolInfoComponent.MaxInstances,
                    HaveUnitPoolInfoComponent = true
                };
            }
            else
            {
                prefabInfo = new PrefabInfo {
                    PrefabGO                  = prefabGO,
                    InitialInstances          = cnf.DefaultInitialInstances,
                    MaxInstances              = cnf.DefaultMaxInstances,
                    HaveUnitPoolInfoComponent = false
                };
            }

            try {
                st.PrefabInfos.Add(prefabGO.GetGOInstanceID(), prefabInfo);
            } catch (ArgumentException) {
                Debug.LogErrorFormat("Duplicate of {0} prefab in {1}", prefabGO.name, nameof(st.PrefabInfos));
            }
        }
Пример #2
0
            protected LoadCharacterConfigTask(string path, Action<T> callbackSuccess, Action callbackFailure) 
                : base(callbackSuccess, callbackFailure) 
            {
                PAssert.IsNotNull(path, nameof(path));

                _path = path;
            }
Пример #3
0
        private PrefabInfo GetPrefabInfo(GameObject prefabGO)
        {
            PAssert.IsNotNull(prefabGO, nameof(prefabGO));

            if (!st.PrefabInfos.TryGetValue(prefabGO.GetGOInstanceID(), out var prefabInfo))
            {
                Debug.LogErrorFormat("Prefab {0} have no {1} in {2}", prefabGO.name, nameof(prefabInfo), nameof(st.PrefabInfos));
                return(null);
            }

            PAssert.IsNotNull(prefabInfo, nameof(prefabInfo));

            return(prefabInfo);
        }
Пример #4
0
        private PrefabInfo GetUnitPrefabInfo(GameObject unitGO)
        {
            PAssert.IsNotNull(unitGO, nameof(unitGO));

            if (!st.UnitGOPrefabInfo.TryGetValue(unitGO, out var prefabInfo))
            {
                Debug.LogErrorFormat("Unit {0} have no {1} in {2}", unitGO.name, nameof(prefabInfo), nameof(st.UnitGOPrefabInfo));
                return(null);
            }

            PAssert.IsNotNull(prefabInfo, nameof(prefabInfo));

            return(prefabInfo);
        }
Пример #5
0
        private Transform GetPrefabTransform(GameObject prefabGO)
        {
            PAssert.IsNotNull(prefabGO, nameof(prefabGO));

            int prefabID = prefabGO.GetGOInstanceID();

            if (!st.PoolTransforms.TryGetValue(prefabID, out var prefabTransform))
            {
                prefabTransform = new GameObject($"{prefabGO.name}_{prefabID}").transform;
                prefabTransform.SetParent(st.PoolMainTransform);
                st.PoolTransforms.Add(prefabID, prefabTransform);
            }

            return(prefabTransform);
        }
Пример #6
0
        private Queue <GameObject> SetDefaultAndGetPoolQueue(GameObject prefabGO)
        {
            PAssert.IsNotNull(prefabGO, nameof(prefabGO));

            int prefabID = prefabGO.GetGOInstanceID();

            if (!st.PoolGO.TryGetValue(prefabID, out var poolUnitsGO))
            {
                poolUnitsGO = new Queue <GameObject>();
                st.PoolGO.Add(prefabID, poolUnitsGO);
            }

            PAssert.IsNotNull(poolUnitsGO, nameof(poolUnitsGO));

            return(poolUnitsGO);
        }
Пример #7
0
        private T GetCacheableUnitComponent <T>(GameObject unitGO) where T : PUnit
        {
            PAssert.IsNotNull(unitGO, nameof(unitGO));

            int unitID = unitGO.GetGOInstanceID();

            if (!st.UnitComponentCache.TryGetValue(unitID, out var unit))
            {
                unit = unitGO.GetComponent <T>();
                st.UnitComponentCache.Add(unitID, unit);
            }

            try {
                return((T)unit);
            } catch (InvalidCastException) {
                return(null);
            }
        }
Пример #8
0
        private Queue <GameObject> GetPoolQueue(GameObject prefabGO)
        {
            PAssert.IsNotNull(prefabGO, nameof(prefabGO));

            if (!st.PoolGO.TryGetValue(prefabGO.GetGOInstanceID(), out var poolUnitsGO))
            {
                return(null);
            }

            PAssert.IsNotNull(poolUnitsGO, nameof(poolUnitsGO));

            if (poolUnitsGO.IsEmpty())
            {
                return(null);
            }

            return(poolUnitsGO);
        }
Пример #9
0
        private bool AddUnitPrefabInfo(GameObject unitGO, PrefabInfo prefabInfo)
        {
            PAssert.IsNotNull(unitGO, nameof(unitGO));
            PAssert.IsNotNull(prefabInfo, nameof(prefabInfo));

            try {
                st.UnitGOPrefabInfo.Add(unitGO, prefabInfo);
            } catch (ArgumentException) {
                Debug.LogErrorFormat(
                    "Duplicate key {0} of {1} in {2}",
                    unitGO.GetGOInstanceID(),
                    unitGO.name,
                    nameof(st.UnitGOPrefabInfo)
                    );
                return(false);
            }

            return(true);
        }
Пример #10
0
        private bool PutUnitIntoPool(GameObject unitGO)
        {
            PAssert.IsNotNull(unitGO, nameof(unitGO));

            var prefabInfo = GetUnitPrefabInfo(unitGO);

            if (prefabInfo == null)
            {
                return(false);
            }

            var poolUnits = SetDefaultAndGetPoolQueue(prefabInfo.PrefabGO);

            if (poolUnits.Count >= prefabInfo.MaxInstances)
            {
                return(false);
            }

            unitGO.SetActive(false);
            unitGO.transform.SetParent(GetPrefabTransform(prefabInfo.PrefabGO));
            poolUnits.Enqueue(unitGO);

            return(true);
        }
Пример #11
0
        private GameObject FetchUnitFromPool(GameObject prefabGO)
        {
            PAssert.IsNotNull(prefabGO, nameof(prefabGO));

            var poolUnitsGO = GetPoolQueue(prefabGO);

            if (poolUnitsGO == null)
            {
                return(null);
            }

            var unitGO = poolUnitsGO.Dequeue();

            if (unitGO == null)
            {
                Debug.LogErrorFormat("Unexpected null {0} in pool for prefab {1}", nameof(unitGO), prefabGO.name);
                return(null);
            }

            unitGO.transform.SetParent(null);
            unitGO.SetActive(true);

            return(unitGO);
        }
Пример #12
0
        private GameObject CreateUnitFromPrefab(GameObject prefabGO)
        {
            PAssert.IsNotNull(prefabGO, nameof(prefabGO));

            var prefabInfo = GetPrefabInfo(prefabGO);

            if (prefabInfo == null)
            {
                return(null);
            }

            GameObject unitGO;

            try {
                unitGO = Instantiate(prefabGO);
            } catch (UnityException e) {
                Debug.LogErrorFormat("Cannot create {0} due to exception", nameof(unitGO));
                Debug.LogException(e);
                return(null);
            }

            if (!AddUnitPrefabInfo(unitGO, prefabInfo))
            {
                InternalDestroyUnit(unitGO);
                return(null);
            }

            if (prefabInfo.HaveUnitPoolInfoComponent)
            {
                unitGO.DestroyComponent <PBaseUnitPoolInfo>();
            }

            unitGO.SetActive(true);

            return(unitGO);
        }
Пример #13
0
 public static bool TryDispose(PBaseAsyncTask self)
 {
     PAssert.IsNotNull(self, nameof(self));
     return(self.TryDispose());
 }
Пример #14
0
        private void RemoveUnitPrefabInfo(GameObject unitGO)
        {
            PAssert.IsNotNull(unitGO, nameof(unitGO));

            st.UnitGOPrefabInfo.Remove(unitGO);
        }