UIComponent.cs 2.9 KB

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