UIComponent.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using UnityEngine;
  5. namespace Model
  6. {
  7. [ObjectEvent]
  8. public class UiComponentSystem : ObjectSystem<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 readonly Dictionary<UIType, IUIFactory> UiTypes = new Dictionary<UIType, IUIFactory>();
  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. this.uis.Clear();
  45. this.UiTypes.Clear();
  46. }
  47. public void Awake()
  48. {
  49. this.Root = GameObject.Find("Global/UI/");
  50. this.Load();
  51. }
  52. public void Load()
  53. {
  54. this.UiTypes.Clear();
  55. Type[] types = DllHelper.GetMonoTypes();
  56. foreach (Type type in types)
  57. {
  58. object[] attrs = type.GetCustomAttributes(typeof (UIFactoryAttribute), false);
  59. if (attrs.Length == 0)
  60. {
  61. continue;
  62. }
  63. UIFactoryAttribute attribute = attrs[0] as UIFactoryAttribute;
  64. if (UiTypes.ContainsKey((UIType)attribute.Type))
  65. {
  66. Log.Debug($"已经存在同类UI Factory: {attribute.Type}");
  67. throw new Exception($"已经存在同类UI Factory: {attribute.Type}");
  68. }
  69. object o = Activator.CreateInstance(type);
  70. IUIFactory factory = o as IUIFactory;
  71. if (factory == null)
  72. {
  73. Log.Error($"{o.GetType().FullName} 没有继承 IUIFactory");
  74. continue;
  75. }
  76. this.UiTypes.Add((UIType)attribute.Type, factory);
  77. }
  78. }
  79. public UI Create(UIType type)
  80. {
  81. try
  82. {
  83. UI ui = UiTypes[type].Create(this.GetParent<Scene>(), type, Root);
  84. uis.Add(type, ui);
  85. // 设置canvas
  86. string cavasName = ui.GameObject.GetComponent<CanvasConfig>().CanvasName;
  87. ui.GameObject.transform.SetParent(this.Root.Get<GameObject>(cavasName).transform, false);
  88. return ui;
  89. }
  90. catch (Exception e)
  91. {
  92. throw new Exception($"{type} UI 错误: {e}");
  93. }
  94. }
  95. public void Add(UIType type, UI ui)
  96. {
  97. this.uis.Add(type, ui);
  98. }
  99. public void Remove(UIType type)
  100. {
  101. UI ui;
  102. if (!uis.TryGetValue(type, out ui))
  103. {
  104. return;
  105. }
  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. }