UIComponent.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Model;
  5. using UnityEngine;
  6. namespace Hotfix
  7. {
  8. /// <summary>
  9. /// 管理所有UI
  10. /// </summary>
  11. [ObjectEvent((int)EntityEventId.UIComponent)]
  12. public class UIComponent: Component, IAwake, ILoad
  13. {
  14. private GameObject 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 (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. public void Awake()
  36. {
  37. this.Root = GameObject.Find("Global/UI/");
  38. this.Load();
  39. }
  40. public void Load()
  41. {
  42. UiTypes = new Dictionary<UIType, IUIFactory>();
  43. Type[] types = DllHelper.GetHotfixTypes();
  44. foreach (Type type in types)
  45. {
  46. object[] attrs = type.GetCustomAttributes(typeof (UIFactoryAttribute), false);
  47. if (attrs.Length == 0)
  48. {
  49. continue;
  50. }
  51. UIFactoryAttribute attribute = attrs[0] as UIFactoryAttribute;
  52. if (UiTypes.ContainsKey((UIType)attribute.Type))
  53. {
  54. Log.Debug($"已经存在同类UI Factory: {attribute.Type}");
  55. throw new Exception($"已经存在同类UI Factory: {attribute.Type}");
  56. }
  57. object o = Activator.CreateInstance(type);
  58. IUIFactory factory = o as IUIFactory;
  59. if (factory == null)
  60. {
  61. Log.Error($"{o.GetType().FullName} 没有继承 IUIFactory");
  62. continue;
  63. }
  64. this.UiTypes.Add((UIType)attribute.Type, factory);
  65. }
  66. }
  67. public UI Create(UIType type)
  68. {
  69. try
  70. {
  71. UI ui = UiTypes[type].Create(GetOwner<Scene>(), type, Root);
  72. uis.Add(type, ui);
  73. // 设置canvas
  74. string cavasName = ui.GameObject.GetComponent<CanvasConfig>().CanvasName;
  75. ui.GameObject.transform.SetParent(this.Root.Get<GameObject>(cavasName).transform, false);
  76. return ui;
  77. }
  78. catch (Exception e)
  79. {
  80. throw new Exception($"{type} UI 错误: {e.ToStr()}");
  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 (!uis.TryGetValue(type, out ui))
  91. {
  92. return;
  93. }
  94. 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. }