UIComponent.cs 2.9 KB

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