UIComponent.cs 3.1 KB

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