| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- namespace WildAttack
- {
- /// <summary>
- /// 气球池
- /// </summary>
- public class BalloonPool : Singleton<BalloonPool>, IObjectPool
- {
- #region Members
- private List<List<GameObject>> balloonObjs;
- private Transform poolTrans;
- #endregion
- #region Override
- public void Init()
- {
- poolTrans = GameObject.Find("BalloonPool").transform;
- balloonObjs = new List<List<GameObject>>();
- // index
- for (int i = 1; i <= 6; i++)
- {
- List<GameObject> balloons = new List<GameObject>();
- // num
- for (int k = 0; k < 5; k++)
- {
- GameObject ball = GameObject.Instantiate<GameObject>(Resources.Load<GameObject>($"StageProperty/balloon_{i}"), poolTrans);
- ball.AddComponent<Balloon>();
- ball.SetActive(false);
- balloons.Add(ball);
- }
- balloonObjs.Add(balloons);
- }
- }
- public GameObject GetGameObject()
- {
- List<GameObject> randomBalloons = balloonObjs[Random.Range(0, 6)];
- for (int i = 0; i < randomBalloons.Count; i++)
- {
- if (!randomBalloons[i].activeInHierarchy)
- {
- return randomBalloons[i];
- }
- }
- int rdmIndex = Random.Range(1, 7);
- GameObject ball = GameObject.Instantiate<GameObject>(Resources.Load<GameObject>($"StageProperty/balloon_{rdmIndex}"), poolTrans);
- ball.AddComponent<Balloon>();
- balloonObjs[rdmIndex - 1].Add(ball);
- return ball;
- }
- public void RecycleAll()
- {
- for (int i = 0; i < poolTrans.childCount; i++)
- {
- if (poolTrans.GetChild(i).gameObject.activeSelf)
- {
- poolTrans.GetChild(i).transform.eulerAngles = Vector3.zero;
- poolTrans.GetChild(i).gameObject.SetActive(false);
- }
- }
- }
- #endregion
- #region Functions
- /// <summary>
- /// 只get红色
- /// </summary>
- /// <returns></returns>
- public GameObject GetRedBalloon()
- {
- List<GameObject> redBalloons = balloonObjs[2];
- for (int i = 0; i < redBalloons.Count; i++)
- {
- if (!redBalloons[i].activeInHierarchy)
- {
- return redBalloons[i];
- }
- }
- GameObject ball = GameObject.Instantiate<GameObject>(Resources.Load<GameObject>("StageProperty/balloon_3"), poolTrans);
- ball.AddComponent<Balloon>();
- balloonObjs[2].Add(ball);
- return ball;
- }
- #endregion
- }
- }
|