UIComponent.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using UnityEngine;
  5. namespace ETModel
  6. {
  7. [ObjectSystem]
  8. public class UiComponentAwakeSystem : AwakeSystem<UIComponent>
  9. {
  10. public override void Awake(UIComponent self)
  11. {
  12. self.Awake();
  13. }
  14. }
  15. [ObjectSystem]
  16. public class UiComponentLoadSystem : LoadSystem<UIComponent>
  17. {
  18. public override void Load(UIComponent self)
  19. {
  20. self.Load();
  21. }
  22. }
  23. /// <summary>
  24. /// 管理所有UI
  25. /// </summary>
  26. public class UIComponent: Component
  27. {
  28. private GameObject Root;
  29. private readonly Dictionary<string, IUIFactory> UiTypes = new Dictionary<string, IUIFactory>();
  30. private readonly Dictionary<string, UI> uis = new Dictionary<string, UI>();
  31. public override void Dispose()
  32. {
  33. if (this.IsDisposed)
  34. {
  35. return;
  36. }
  37. base.Dispose();
  38. foreach (string type in uis.Keys.ToArray())
  39. {
  40. UI ui;
  41. if (!uis.TryGetValue(type, out ui))
  42. {
  43. continue;
  44. }
  45. uis.Remove(type);
  46. ui.Dispose();
  47. }
  48. this.uis.Clear();
  49. this.UiTypes.Clear();
  50. }
  51. public void Awake()
  52. {
  53. this.Root = GameObject.Find("Global/UI/");
  54. this.Load();
  55. }
  56. public void Load()
  57. {
  58. this.UiTypes.Clear();
  59. List<Type> types = Game.EventSystem.GetTypes(typeof(UIFactoryAttribute));
  60. foreach (Type type in types)
  61. {
  62. object[] attrs = type.GetCustomAttributes(typeof (UIFactoryAttribute), false);
  63. if (attrs.Length == 0)
  64. {
  65. continue;
  66. }
  67. UIFactoryAttribute attribute = attrs[0] as UIFactoryAttribute;
  68. if (UiTypes.ContainsKey(attribute.Type))
  69. {
  70. Log.Debug($"已经存在同类UI Factory: {attribute.Type}");
  71. throw new Exception($"已经存在同类UI Factory: {attribute.Type}");
  72. }
  73. object o = Activator.CreateInstance(type);
  74. IUIFactory factory = o as IUIFactory;
  75. if (factory == null)
  76. {
  77. Log.Error($"{o.GetType().FullName} 没有继承 IUIFactory");
  78. continue;
  79. }
  80. this.UiTypes.Add(attribute.Type, factory);
  81. }
  82. }
  83. public UI Create(string type)
  84. {
  85. try
  86. {
  87. UI ui = UiTypes[type].Create(this.GetParent<Scene>(), type, Root);
  88. uis.Add(type, ui);
  89. // 设置canvas
  90. string cavasName = ui.GameObject.GetComponent<CanvasConfig>().CanvasName;
  91. ui.GameObject.transform.SetParent(this.Root.Get<GameObject>(cavasName).transform, false);
  92. return ui;
  93. }
  94. catch (Exception e)
  95. {
  96. throw new Exception($"{type} UI 错误: {e}");
  97. }
  98. }
  99. public void Add(string type, UI ui)
  100. {
  101. this.uis.Add(type, ui);
  102. }
  103. public void Remove(string type)
  104. {
  105. UI ui;
  106. if (!uis.TryGetValue(type, out ui))
  107. {
  108. return;
  109. }
  110. uis.Remove(type);
  111. ui.Dispose();
  112. }
  113. public void RemoveAll()
  114. {
  115. foreach (string type in this.uis.Keys.ToArray())
  116. {
  117. UI ui;
  118. if (!this.uis.TryGetValue(type, out ui))
  119. {
  120. continue;
  121. }
  122. this.uis.Remove(type);
  123. ui.Dispose();
  124. }
  125. }
  126. public UI Get(string type)
  127. {
  128. UI ui;
  129. this.uis.TryGetValue(type, out ui);
  130. return ui;
  131. }
  132. public List<string> GetUITypeList()
  133. {
  134. return new List<string>(this.uis.Keys);
  135. }
  136. }
  137. }