UIComponent.cs 2.7 KB

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