DrawFactory.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. namespace WXB
  6. {
  7. public static class DrawFactory
  8. {
  9. static DrawFactory()
  10. {
  11. factorys.Add(DrawType.Outline, new Factory<OutlineDraw>());
  12. factorys.Add(DrawType.Alpha, new Factory<AlphaDraw>());
  13. factorys.Add(DrawType.Offset, new Factory<OffsetDraw>());
  14. factorys.Add(DrawType.OffsetAndAlpha, new Factory<AlphaOffsetDraw>());
  15. factorys.Add(DrawType.Cartoon, new Factory<CartoonDraw>());
  16. }
  17. public interface IFactory
  18. {
  19. Draw Create(GameObject go);
  20. void Free(Draw d);
  21. }
  22. static int s_total = 0;
  23. public static class Pool<T> where T : MonoBehaviour, Draw, new ()
  24. {
  25. public static List<T> EmptyList = new List<T>();
  26. public static List<T> UsedList = new List<T>();
  27. public static T GetOrCreate(GameObject parent)
  28. {
  29. T obj = null;
  30. while (EmptyList.Count != 0)
  31. {
  32. obj = EmptyList.pop_back();
  33. if (obj != null)
  34. break;
  35. }
  36. if (obj == null)
  37. {
  38. string name = (++s_total).ToString();
  39. #if UNITY_EDITOR
  40. // If we're in the editor, create the game object with hide flags set right away
  41. GameObject go = UnityEditor.EditorUtility.CreateGameObjectWithHideFlags(name, HideFlags.DontSave);
  42. obj = go.AddComponent<T>();
  43. parent.AddChild(go);
  44. obj.name = string.Format("{0}-{1}", typeof(T).Name, name);
  45. #else
  46. obj = parent.AddChild<T>();
  47. obj.name = name;
  48. #endif
  49. }
  50. obj.transform.SetParent(parent.transform);
  51. obj.gameObject.layer = parent.layer;
  52. obj.OnInit();
  53. return obj;
  54. }
  55. public static void Free(T d)
  56. {
  57. d.Release();
  58. UsedList.Remove(d);
  59. if (EmptyList.Count <= 10)
  60. {
  61. EmptyList.Add(d);
  62. }
  63. else
  64. {
  65. d.DestroySelf();
  66. }
  67. #if UNITY_EDITOR
  68. //if (!Application.isPlaying)
  69. //{
  70. // for (int i = 0; i < EmptyList.Count; ++i)
  71. // {
  72. // if (EmptyList[i] != null)
  73. // EmptyList[i].DestroySelf();
  74. // }
  75. // EmptyList.Clear();
  76. //}
  77. #endif
  78. }
  79. }
  80. public class Factory<T> : IFactory where T : MonoBehaviour, Draw, new()
  81. {
  82. public Draw Create(GameObject go)
  83. {
  84. return Pool<T>.GetOrCreate(go);
  85. }
  86. public void Free(Draw d)
  87. {
  88. Pool<T>.Free((T)d);
  89. }
  90. }
  91. static IFactory DefaultFactory = new Factory<DrawObject>();
  92. static Dictionary<DrawType, IFactory> factorys = new Dictionary<DrawType, IFactory>();
  93. static IFactory Get(DrawType type)
  94. {
  95. if (type == 0)
  96. return DefaultFactory;
  97. IFactory f = null;
  98. if (factorys.TryGetValue(type, out f))
  99. return f;
  100. Debug.LogErrorFormat("type:{0} not find!", type);
  101. return null;
  102. }
  103. static public Draw Create(GameObject go, DrawType type)
  104. {
  105. IFactory f = Get(type);
  106. if (f == null)
  107. return null;
  108. return f.Create(go);
  109. }
  110. static public void Free(Draw go)
  111. {
  112. if (go == null)
  113. return;
  114. IFactory f = Get(go.type);
  115. if (f == null)
  116. {
  117. go.DestroySelf();
  118. return;
  119. }
  120. f.Free(go);
  121. }
  122. }
  123. }