UIComponent.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Reflection;
  5. using Base;
  6. using UnityEngine;
  7. namespace Model
  8. {
  9. [DisposerEvent]
  10. public class UIComponentEvent : DisposerEvent<UIComponent>, IAwake, ILoader
  11. {
  12. public void Load()
  13. {
  14. UIComponent component = GetValue();
  15. component.Load();
  16. }
  17. public void Awake()
  18. {
  19. this.GetValue().Awake();
  20. }
  21. }
  22. /// <summary>
  23. /// 管理所有UI
  24. /// </summary>
  25. public class UIComponent: Component
  26. {
  27. private UI Root;
  28. private Dictionary<UIType, IUIFactory> UiTypes;
  29. private readonly Dictionary<UIType, UI> uis = new Dictionary<UIType, UI>();
  30. public override void Dispose()
  31. {
  32. if (this.Id == 0)
  33. {
  34. return;
  35. }
  36. base.Dispose();
  37. foreach (UIType type in uis.Keys.ToArray())
  38. {
  39. UI ui;
  40. if (!uis.TryGetValue(type, out ui))
  41. {
  42. continue;
  43. }
  44. uis.Remove(type);
  45. ui.Dispose();
  46. }
  47. }
  48. public void Awake()
  49. {
  50. GameObject uiCanvas = GameObject.Find("/UICanvas");
  51. uiCanvas.GetComponent<Canvas>().worldCamera = GameObject.Find("/Camera").GetComponent<Camera>();
  52. this.Root = new UI(this.GetOwner<Scene>(), UIType.Root, null, uiCanvas);
  53. this.Load();
  54. }
  55. public void Load()
  56. {
  57. this.UiTypes = new Dictionary<UIType, IUIFactory>();
  58. Assembly[] assemblies = Game.DisposerEventManager.GetAssemblies();
  59. foreach (Assembly assembly in assemblies)
  60. {
  61. Type[] types = assembly.GetTypes();
  62. foreach (Type type in types)
  63. {
  64. object[] attrs = type.GetCustomAttributes(typeof(UIFactoryAttribute), false);
  65. if (attrs.Length == 0)
  66. {
  67. continue;
  68. }
  69. UIFactoryAttribute attribute = attrs[0] as UIFactoryAttribute;
  70. if (this.UiTypes.ContainsKey(attribute.Type))
  71. {
  72. throw new GameException($"已经存在同类UI Factory: {attribute.Type}");
  73. }
  74. IUIFactory iIuiFactory = Activator.CreateInstance(type) as IUIFactory;
  75. if (iIuiFactory == null)
  76. {
  77. throw new GameException("UI Factory没有继承IUIFactory");
  78. }
  79. this.UiTypes.Add(attribute.Type, iIuiFactory);
  80. }
  81. }
  82. }
  83. public UI Create(UIType type)
  84. {
  85. try
  86. {
  87. UI ui = this.UiTypes[type].Create(this.GetOwner<Scene>(), type, this.Root);
  88. this.uis.Add(type, ui);
  89. return ui;
  90. }
  91. catch (Exception e)
  92. {
  93. throw new Exception($"{type} UI 错误: {e}");
  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 (!this.uis.TryGetValue(type, out ui))
  104. {
  105. return;
  106. }
  107. this.uis.Remove(type);
  108. ui.Dispose();
  109. }
  110. public void RemoveAll()
  111. {
  112. foreach (UIType type in this.uis.Keys.ToArray())
  113. {
  114. UI ui;
  115. if (!this.uis.TryGetValue(type, out ui))
  116. {
  117. continue;
  118. }
  119. this.uis.Remove(type);
  120. ui.Dispose();
  121. }
  122. }
  123. public UI Get(UIType type)
  124. {
  125. UI ui;
  126. this.uis.TryGetValue(type, out ui);
  127. return ui;
  128. }
  129. public List<UIType> GetUITypeList()
  130. {
  131. return new List<UIType>(this.uis.Keys);
  132. }
  133. }
  134. }