You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

115 lines
2.7 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace CIG
  5. {
  6. public class ObjectPool : MonoBehaviour
  7. {
  8. private void Awake()
  9. {
  10. this._availablePool = new List<GameObject>(this._initialPoolCount);
  11. this._claimedPool = new List<GameObject>(this._initialPoolCount);
  12. for (int i = 0; i < this._initialPoolCount; i++)
  13. {
  14. GameObject gameObject = UnityEngine.Object.Instantiate<GameObject>(this._prefab, base.transform, false);
  15. gameObject.SetActive(false);
  16. this._availablePool.Add(gameObject);
  17. }
  18. }
  19. public T Pop<T>(Transform parent) where T : MonoBehaviour
  20. {
  21. GameObject gameObject = this.Pop(base.transform);
  22. return gameObject.GetComponent<T>();
  23. }
  24. public GameObject Pop(Transform parent)
  25. {
  26. GameObject gameObject;
  27. if (this._availablePool.Count == 0)
  28. {
  29. UnityEngine.Debug.LogWarning("Failed to pop an instance from the Pool, no more instances available. - Instantiating a new one !");
  30. gameObject = UnityEngine.Object.Instantiate<GameObject>(this._prefab, base.transform, false);
  31. }
  32. else
  33. {
  34. gameObject = this._availablePool[0];
  35. this._availablePool.RemoveAt(0);
  36. }
  37. this._claimedPool.Add(gameObject);
  38. gameObject.SetActive(true);
  39. gameObject.transform.SetParent(parent, false);
  40. return gameObject;
  41. }
  42. public void Push(List<GameObject> instances)
  43. {
  44. int count = instances.Count;
  45. for (int i = 0; i < count; i++)
  46. {
  47. this.Push(instances[i]);
  48. }
  49. }
  50. public void Push(GameObject instance)
  51. {
  52. if (!this._claimedPool.Remove(instance))
  53. {
  54. if (!this._availablePool.Contains(instance))
  55. {
  56. UnityEngine.Debug.LogWarningFormat("Failed to push '{0}' back to the pool, it doesn't belong to this ObjectPool!", new object[]
  57. {
  58. instance.name
  59. });
  60. }
  61. else
  62. {
  63. UnityEngine.Debug.LogWarningFormat("Failed to push '{0}' back to the pool, it was never popped!", new object[]
  64. {
  65. instance.name
  66. });
  67. }
  68. return;
  69. }
  70. instance.transform.SetParent(base.transform, false);
  71. instance.SetActive(false);
  72. this._availablePool.Add(instance);
  73. }
  74. public int AvailableInstances
  75. {
  76. get
  77. {
  78. return this._availablePool.Count;
  79. }
  80. }
  81. public int ClaimedInstances
  82. {
  83. get
  84. {
  85. return this._claimedPool.Count;
  86. }
  87. }
  88. public int TotalInstances
  89. {
  90. get
  91. {
  92. return this.AvailableInstances + this.ClaimedInstances;
  93. }
  94. }
  95. [SerializeField]
  96. private GameObject _prefab;
  97. [SerializeField]
  98. private int _initialPoolCount;
  99. private List<GameObject> _availablePool;
  100. private List<GameObject> _claimedPool;
  101. }
  102. }