UIComponent.cs 2.7 KB

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