UIComponent.cs 3.0 KB

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