GameObjectDestroyPool.cs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #if !COMBAT_SERVER
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using Utility;
  5. namespace CombatLibrary.CombatLibrary.CombatCore.Utility
  6. {
  7. public class GameObjectDestroyPool : Singleton<GameObjectDestroyPool>
  8. {
  9. private BetterList<GameObject> pool = new BetterList<GameObject>();
  10. private List<GameObject> _removeGameObjecet = new List<GameObject>();
  11. public GameObjectDestroyPool()
  12. {
  13. StaticUpdater.Instance.AddRenderUpdateCallBack(UpdateDelete);
  14. }
  15. private int maxCount =1;
  16. public void RomoveDestroyObject(GameObject goObj)
  17. {
  18. if (pool.Contains(goObj))
  19. {
  20. pool.Remove(goObj);
  21. }
  22. if (_removeGameObjecet.Contains(goObj))
  23. {
  24. _removeGameObjecet.Remove(goObj);
  25. }
  26. }
  27. public void DestroyObject(GameObject goObj)
  28. {
  29. if (pool.Contains(goObj)||goObj==null)
  30. {
  31. return;
  32. }
  33. goObj.SetActive(false);
  34. pool.Add(goObj);
  35. }
  36. public void ImmediatelyDestroyObject(GameObject goObj)
  37. {
  38. if (!goObj)
  39. {
  40. return;
  41. }
  42. GameObject.Destroy(goObj);
  43. }
  44. public override void Dispose()
  45. {
  46. StaticUpdater.Instance.RemoveRenderUpdateCallBack(UpdateDelete);
  47. for (int i = 0; i < pool.Count; i++)
  48. {
  49. ImmediatelyDestroyObject(pool[i]);
  50. }
  51. pool.Clear();
  52. base.Dispose();
  53. }
  54. public void UpdateDelete()
  55. {
  56. _removeGameObjecet.Clear();
  57. int count = 0;
  58. for (int i = 0; i < pool.Count; i++)
  59. {
  60. if (count <= maxCount)
  61. {
  62. _removeGameObjecet.Add(pool[i]);
  63. pool.RemoveAt(i);
  64. i--;
  65. count++;
  66. }
  67. }
  68. int removeCount = 0;
  69. while (_removeGameObjecet.Count>0&&removeCount<maxCount+20)
  70. {
  71. removeCount++;
  72. GameObject g = _removeGameObjecet[0];
  73. ImmediatelyDestroyObject(g);
  74. _removeGameObjecet.RemoveAt(0);
  75. }
  76. }
  77. }
  78. }
  79. #endif