PoolingManager.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. namespace HQFPSWeapons
  4. {
  5. public class PoolingManager : Singleton<PoolingManager>
  6. {
  7. private Dictionary<string, ObjectPool> m_Pools = new Dictionary<string, ObjectPool>(50);
  8. private SortedList<float, PoolableObject> m_ObjectsToRelease = new SortedList<float, PoolableObject>();
  9. public ObjectPool CreatePool(GameObject template, int minSize, int maxSize, bool autoShrink, string poolId, float autoReleaseDelay = Mathf.Infinity)
  10. {
  11. ObjectPool pool;
  12. if (!m_Pools.TryGetValue(poolId, out pool))
  13. {
  14. pool = new ObjectPool(poolId, template, minSize, maxSize, autoShrink, autoReleaseDelay, transform);
  15. m_Pools.Add(poolId, pool);
  16. }
  17. return pool;
  18. }
  19. /// <summary>
  20. /// This method will use the prefab's instance id as a poolId to create a pool if one doesn't exist (it's the closest thing in ease of use to Object.Instantiate())<br></br>
  21. /// You can also use CreatePool() to create a custom pool for your prefabs.
  22. /// </summary>
  23. /// <returns></returns>
  24. public PoolableObject GetObject(GameObject prefab, Vector3 position, Quaternion rotation, Transform parent)
  25. {
  26. PoolableObject obj = null;
  27. if (prefab != null)
  28. {
  29. ObjectPool pool;
  30. if (m_Pools.TryGetValue(prefab.GetInstanceID().ToString(), out pool))
  31. {
  32. obj = pool.GetObject();
  33. }
  34. else
  35. {
  36. pool = CreatePool(prefab, 10, 30, true, prefab.GetInstanceID().ToString());
  37. obj = pool.GetObject();
  38. }
  39. }
  40. if (obj != null)
  41. {
  42. obj.transform.SetPositionAndRotation(position, rotation);
  43. obj.transform.SetParent(parent);
  44. }
  45. return obj;
  46. }
  47. /// <summary>
  48. /// This method will use the prefab's instance id as a poolId to create a pool if one doesn't exist (it's the closest thing in ease of use to Object.Instantiate())<br></br>
  49. /// You can also use CreatePool() to create a custom pool for your prefabs.
  50. /// </summary>
  51. /// <returns></returns>
  52. public PoolableObject GetObject(GameObject prefab, Vector3 position, Quaternion rotation)
  53. {
  54. PoolableObject obj = null;
  55. if (prefab != null)
  56. {
  57. ObjectPool pool;
  58. if (m_Pools.TryGetValue(prefab.GetInstanceID().ToString(), out pool))
  59. {
  60. obj = pool.GetObject();
  61. }
  62. else
  63. {
  64. pool = CreatePool(prefab, 10, 30, true, prefab.GetInstanceID().ToString());
  65. obj = pool.GetObject();
  66. }
  67. }
  68. if (obj != null)
  69. obj.transform.SetPositionAndRotation(position, rotation);
  70. return obj;
  71. }
  72. public PoolableObject GetObject(string poolId, Vector3 position, Quaternion rotation, Transform parent)
  73. {
  74. PoolableObject obj = GetObject(poolId);
  75. if (obj != null)
  76. {
  77. obj.transform.SetPositionAndRotation(position, rotation);
  78. obj.transform.SetParent(parent);
  79. }
  80. return obj;
  81. }
  82. public PoolableObject GetObject(string poolId, Vector3 position, Quaternion rotation)
  83. {
  84. PoolableObject obj = GetObject(poolId);
  85. if (obj != null)
  86. obj.transform.SetPositionAndRotation(position, rotation);
  87. return obj;
  88. }
  89. public PoolableObject GetObject(string poolId)
  90. {
  91. ObjectPool pool = null;
  92. m_Pools.TryGetValue(poolId, out pool);
  93. if(pool != null)
  94. return pool.GetObject();
  95. else
  96. return null;
  97. }
  98. public bool ReleaseObject(PoolableObject obj)
  99. {
  100. if(obj == null)
  101. return false;
  102. ObjectPool pool = null;
  103. if(!m_Pools.ContainsKey(obj.PoolId))
  104. print("key not found: " + obj.PoolId);
  105. m_Pools.TryGetValue(obj.PoolId, out pool);
  106. if(pool != null)
  107. return pool.TryPoolObject(obj);
  108. else
  109. return false;
  110. }
  111. public void QueueObjectRelease(PoolableObject obj, float delay)
  112. {
  113. float key = Time.time + delay;
  114. if(m_ObjectsToRelease.ContainsKey(key))
  115. key += Random.Range(0.05f, 0.5f);
  116. m_ObjectsToRelease.Add(key, obj);
  117. }
  118. private void Update()
  119. {
  120. if(m_ObjectsToRelease.Count > 0 && Time.time > m_ObjectsToRelease.Keys[0])
  121. {
  122. ReleaseObject(m_ObjectsToRelease.Values[0]);
  123. m_ObjectsToRelease.RemoveAt(0);
  124. }
  125. }
  126. }
  127. }