コード例 #1
0
        private void SpawnZombie(int id, Transform poolTransform)
        {
            if (_alreadyEnergy.Contains(id))
            {
                return;
            }

            _alreadyEnergy.Add(id);
            List <Transform> spawnPoints = FindAllChildrenRecursiveWithTag(poolTransform, Tag.ZombieSpawn);

            spawnPoints.Shuffle();

            for (int i = 0; i < spawnPoints.Count && i < ZombieSpawnCount; i++)
            {
                Transform   point      = spawnPoints[i];
                IPoolObject poolingObj = _zombiePool.Get();
                poolingObj.PoolTransform.position = point.transform.position;

                // set random zombie sprite
                // need to refactor it
                SpriteRenderer zombieSpriteRenderer = poolingObj.PoolTransform.GetComponentInChildren <SpriteRenderer> ();
                int            randomZombieNumber   = Random.Range(0, ZombiePrefabs.Length);
                zombieSpriteRenderer.sprite = ZombiePrefabs [randomZombieNumber].GetComponentInChildren <SpriteRenderer> ().sprite;

                // finally activate
                poolingObj.PoolTransform.gameObject.SetActive(true);
            }
        }
コード例 #2
0
ファイル: AssetsPooling.cs プロジェクト: firefishes/ShipDock
        /// <summary>
        /// 以游戏物体或预设为模板,从对象池中获取闲置对象
        /// </summary>
        public GameObject FromPool(int poolName, ref GameObject template, OnGameObjectPoolItem onInit = null, bool visible = true)
        {
            GameObject result = null;
            bool       flag   = CheckPoolResult(poolName, ref mPool, ref result);

            if (result == null)
            {
                result = Object.Instantiate(template);
            }
            else
            {
                if (flag && (PoolContainer != null))
                {
                    PoolContainer.Get(result);
                }
                else
                {
                }
            }

            result.SetActive(visible);
            onInit?.Invoke(result);

            return(result);
        }
コード例 #3
0
        /// <summary>
        /// 以脚本类型为模板,从对象池中获取闲置对象
        /// </summary>
        public T FromComponentPool <T>(int poolName, ref T template, OnComponentPoolItem <T> onInit = null, bool visible = true) where T : Component
        {
            Tester.Instance.Log(TesterBaseApp.Instance, TesterBaseApp.LOG, template == null, "error: template is null, the type is ".Append(typeof(T).ToString()));

            Component tempResult = null;
            bool      flag       = CheckPoolResult(poolName, ref mCompPool, ref tempResult);

            if (tempResult == null)
            {
                tempResult = Object.Instantiate(template);
            }
            else
            {
                if (flag && (PoolContainer != null))
                {
                    PoolContainer.Get(tempResult.gameObject);
                }
            }

            T result = (T)tempResult;

            result.gameObject.SetActive(visible);
            onInit?.Invoke(result);

            return(result);
        }
コード例 #4
0
        private void SpawnCoin(int id, IPoolObject obj)
        {
            if (_alreadyCoins.Contains(id))
            {
                return;
            }

            _alreadyCoins.Add(id);
            List <Transform> spawnPoints = FindAllChildrenRecursiveWithTag(obj.PoolTransform, Tag.CoinSpawn);

            spawnPoints.Shuffle();

            for (int i = 0; i < spawnPoints.Count && i < CoinSpawnCount; i++)
            {
                var point      = spawnPoints[i];
                var poolingObj = _coinsPool.Get();
                poolingObj.PoolTransform.position = point.transform.position;
                poolingObj.PoolTransform.gameObject.SetActive(true);
            }
        }
コード例 #5
0
        IEnumerator Start()
        {
            if (_pool == null)
            {
                yield break;
            }
            var         waiter = new WaitForSeconds(SpawnDelay);
            IPoolObject obj;

            while (true)
            {
                obj = _pool.Get();
                obj.PoolTransform.localPosition = new Vector3(
                    Mathf.Lerp(-1f, 1f, Rng.GetFloatStatic()), Mathf.Lerp(-1f, 1f, Rng.GetFloatStatic()), 0f);
                obj.PoolTransform.localRotation =
                    Quaternion.Euler(new Vector3(0f, Mathf.Lerp(-180f, 1f, Rng.GetFloatStatic()), 0f));
                obj.PoolTransform.gameObject.SetActive(true);
                yield return(waiter);
            }
        }