BalloonPool.cs 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace WildAttack
  5. {
  6. /// <summary>
  7. /// 气球池
  8. /// </summary>
  9. public class BalloonPool : Singleton<BalloonPool>, IObjectPool
  10. {
  11. #region Members
  12. private List<List<GameObject>> balloonObjs;
  13. private Transform poolTrans;
  14. #endregion
  15. #region Override
  16. public void Init()
  17. {
  18. poolTrans = GameObject.Find("BalloonPool").transform;
  19. balloonObjs = new List<List<GameObject>>();
  20. // index
  21. for (int i = 1; i <= 6; i++)
  22. {
  23. List<GameObject> balloons = new List<GameObject>();
  24. // num
  25. for (int k = 0; k < 5; k++)
  26. {
  27. GameObject ball = GameObject.Instantiate<GameObject>(Resources.Load<GameObject>($"StageProperty/balloon_{i}"), poolTrans);
  28. ball.AddComponent<Balloon>();
  29. ball.SetActive(false);
  30. balloons.Add(ball);
  31. }
  32. balloonObjs.Add(balloons);
  33. }
  34. }
  35. public GameObject GetGameObject()
  36. {
  37. List<GameObject> randomBalloons = balloonObjs[Random.Range(0, 6)];
  38. for (int i = 0; i < randomBalloons.Count; i++)
  39. {
  40. if (!randomBalloons[i].activeInHierarchy)
  41. {
  42. return randomBalloons[i];
  43. }
  44. }
  45. int rdmIndex = Random.Range(1, 7);
  46. GameObject ball = GameObject.Instantiate<GameObject>(Resources.Load<GameObject>($"StageProperty/balloon_{rdmIndex}"), poolTrans);
  47. ball.AddComponent<Balloon>();
  48. balloonObjs[rdmIndex - 1].Add(ball);
  49. return ball;
  50. }
  51. public void RecycleAll()
  52. {
  53. for (int i = 0; i < poolTrans.childCount; i++)
  54. {
  55. if (poolTrans.GetChild(i).gameObject.activeSelf)
  56. {
  57. poolTrans.GetChild(i).transform.eulerAngles = Vector3.zero;
  58. poolTrans.GetChild(i).gameObject.SetActive(false);
  59. }
  60. }
  61. }
  62. #endregion
  63. #region Functions
  64. /// <summary>
  65. /// 只get红色
  66. /// </summary>
  67. /// <returns></returns>
  68. public GameObject GetRedBalloon()
  69. {
  70. List<GameObject> redBalloons = balloonObjs[2];
  71. for (int i = 0; i < redBalloons.Count; i++)
  72. {
  73. if (!redBalloons[i].activeInHierarchy)
  74. {
  75. return redBalloons[i];
  76. }
  77. }
  78. GameObject ball = GameObject.Instantiate<GameObject>(Resources.Load<GameObject>("StageProperty/balloon_3"), poolTrans);
  79. ball.AddComponent<Balloon>();
  80. balloonObjs[2].Add(ball);
  81. return ball;
  82. }
  83. #endregion
  84. }
  85. }