UIComponent.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Model;
  5. using UnityEngine;
  6. namespace Hotfix
  7. {
  8. [ObjectEvent]
  9. public class UIComponentEvent : ObjectEvent<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 Dictionary<UIType, IUIFactory> UiTypes;
  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. }
  46. public void Awake()
  47. {
  48. this.Root = GameObject.Find("Global/UI/");
  49. this.Load();
  50. }
  51. public void Load()
  52. {
  53. UiTypes = new Dictionary<UIType, IUIFactory>();
  54. Type[] types = DllHelper.GetHotfixTypes();
  55. foreach (Type type in types)
  56. {
  57. object[] attrs = type.GetCustomAttributes(typeof (UIFactoryAttribute), false);
  58. if (attrs.Length == 0)
  59. {
  60. continue;
  61. }
  62. UIFactoryAttribute attribute = attrs[0] as UIFactoryAttribute;
  63. if (UiTypes.ContainsKey((UIType)attribute.Type))
  64. {
  65. Log.Debug($"已经存在同类UI Factory: {attribute.Type}");
  66. throw new Exception($"已经存在同类UI Factory: {attribute.Type}");
  67. }
  68. object o = Activator.CreateInstance(type);
  69. IUIFactory factory = o as IUIFactory;
  70. if (factory == null)
  71. {
  72. Log.Error($"{o.GetType().FullName} 没有继承 IUIFactory");
  73. continue;
  74. }
  75. this.UiTypes.Add((UIType)attribute.Type, factory);
  76. }
  77. }
  78. public UI Create(UIType type)
  79. {
  80. try
  81. {
  82. UI ui = UiTypes[type].Create(this.GetEntity<Scene>(), type, Root);
  83. uis.Add(type, ui);
  84. // 设置canvas
  85. string cavasName = ui.GameObject.GetComponent<CanvasConfig>().CanvasName;
  86. ui.GameObject.transform.SetParent(this.Root.Get<GameObject>(cavasName).transform, false);
  87. return ui;
  88. }
  89. catch (Exception e)
  90. {
  91. throw new Exception($"{type} UI 错误: {e.ToStr()}");
  92. }
  93. }
  94. public void Add(UIType type, UI ui)
  95. {
  96. this.uis.Add(type, ui);
  97. }
  98. public void Remove(UIType type)
  99. {
  100. UI ui;
  101. if (!uis.TryGetValue(type, out ui))
  102. {
  103. return;
  104. }
  105. UiTypes[type].Remove(type);
  106. uis.Remove(type);
  107. ui.Dispose();
  108. }
  109. public void RemoveAll()
  110. {
  111. foreach (UIType type in this.uis.Keys.ToArray())
  112. {
  113. UI ui;
  114. if (!this.uis.TryGetValue(type, out ui))
  115. {
  116. continue;
  117. }
  118. this.uis.Remove(type);
  119. ui.Dispose();
  120. }
  121. }
  122. public UI Get(UIType type)
  123. {
  124. UI ui;
  125. this.uis.TryGetValue(type, out ui);
  126. return ui;
  127. }
  128. public List<UIType> GetUITypeList()
  129. {
  130. return new List<UIType>(this.uis.Keys);
  131. }
  132. }
  133. }