ObjectPool.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Reflection;
  6. using System.Text;
  7. using UnityEngine;
  8. namespace ETModel
  9. {
  10. public class ComponentQueue: Object
  11. {
  12. public string TypeName { get; }
  13. private readonly Queue<Object> queue = new Queue<Object>();
  14. public ComponentQueue(string typeName)
  15. {
  16. this.TypeName = typeName;
  17. }
  18. public void Enqueue(Object entity)
  19. {
  20. this.queue.Enqueue(entity);
  21. }
  22. public Object Dequeue()
  23. {
  24. return this.queue.Dequeue();
  25. }
  26. public Object Peek()
  27. {
  28. return this.queue.Peek();
  29. }
  30. public Queue<Object> Queue
  31. {
  32. get
  33. {
  34. return this.queue;
  35. }
  36. }
  37. public int Count
  38. {
  39. get
  40. {
  41. return this.queue.Count;
  42. }
  43. }
  44. public override void Dispose()
  45. {
  46. while (this.queue.Count > 0)
  47. {
  48. Object component = this.queue.Dequeue();
  49. component.Dispose();
  50. }
  51. }
  52. }
  53. public class ObjectPool: Object
  54. {
  55. private static ObjectPool instance;
  56. public static ObjectPool Instance
  57. {
  58. get
  59. {
  60. if (instance == null)
  61. {
  62. instance = new ObjectPool();
  63. }
  64. return instance;
  65. }
  66. }
  67. public readonly Dictionary<Type, ComponentQueue> dictionary = new Dictionary<Type, ComponentQueue>();
  68. public Object Fetch(Type type)
  69. {
  70. Object obj;
  71. if (!this.dictionary.TryGetValue(type, out ComponentQueue queue))
  72. {
  73. obj = (Object)Activator.CreateInstance(type);
  74. }
  75. else if (queue.Count == 0)
  76. {
  77. obj = (Object)Activator.CreateInstance(type);
  78. }
  79. else
  80. {
  81. obj = queue.Dequeue();
  82. }
  83. return obj;
  84. }
  85. public T Fetch<T>() where T: Object
  86. {
  87. T t = (T) this.Fetch(typeof(T));
  88. return t;
  89. }
  90. public void Recycle(Object obj)
  91. {
  92. Type type = obj.GetType();
  93. ComponentQueue queue;
  94. if (!this.dictionary.TryGetValue(type, out queue))
  95. {
  96. queue = new ComponentQueue(type.Name);
  97. #if UNITY_EDITOR
  98. if (queue.ViewGO != null)
  99. {
  100. queue.ViewGO.transform.SetParent(this.ViewGO.transform);
  101. queue.ViewGO.name = $"{type.Name}s";
  102. }
  103. #endif
  104. this.dictionary.Add(type, queue);
  105. }
  106. #if UNITY_EDITOR
  107. if (obj.ViewGO != null)
  108. {
  109. obj.ViewGO.transform.SetParent(queue.ViewGO.transform);
  110. }
  111. #endif
  112. queue.Enqueue(obj);
  113. }
  114. public override void Dispose()
  115. {
  116. foreach (var kv in this.dictionary)
  117. {
  118. kv.Value.Dispose();
  119. }
  120. this.dictionary.Clear();
  121. instance = null;
  122. }
  123. }
  124. }