UIComponent.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Reflection;
  5. using ILRuntime.CLR.Method;
  6. using Model;
  7. using UnityEngine;
  8. namespace Hotfix
  9. {
  10. #if ILRuntime
  11. public class IILUIFactoryMethod : IUIFactory
  12. {
  13. private readonly object instance;
  14. private readonly IMethod methodInfo;
  15. private readonly object[] params3 = new object[3];
  16. public IILUIFactoryMethod(Type type)
  17. {
  18. this.instance = Activator.CreateInstance(type);
  19. this.methodInfo = DllHelper.GetType(type.FullName).GetMethod("Create", 3);
  20. }
  21. public UI Create(Scene scene, UIType type, GameObject parent)
  22. {
  23. this.params3[0] = scene;
  24. this.params3[1] = type;
  25. this.params3[2] = parent;
  26. return (UI)Model.Init.Instance.AppDomain.Invoke(methodInfo, instance, params3);
  27. }
  28. }
  29. #else
  30. public class IMonoUIFactoryMethod : IUIFactory
  31. {
  32. private readonly object instance;
  33. private readonly MethodInfo methodInfo;
  34. private readonly object[] params3 = new object[3];
  35. public IMonoUIFactoryMethod(Type type)
  36. {
  37. this.instance = Activator.CreateInstance(type);
  38. this.methodInfo = type.GetMethod("Create");
  39. }
  40. public UI Create(Scene scene, UIType type, GameObject parent)
  41. {
  42. this.params3[0] = scene;
  43. this.params3[1] = type;
  44. this.params3[2] = parent;
  45. return (UI)this.methodInfo.Invoke(this.instance, this.params3);
  46. }
  47. }
  48. #endif
  49. /// <summary>
  50. /// 管理所有UI
  51. /// </summary>
  52. [ObjectEvent((int)EntityEventId.UIComponent)]
  53. public class UIComponent: Component, IAwake, ILoad
  54. {
  55. private GameObject Root;
  56. private Dictionary<UIType, IUIFactory> UiTypes;
  57. private readonly Dictionary<UIType, UI> uis = new Dictionary<UIType, UI>();
  58. public override void Dispose()
  59. {
  60. if (Id == 0)
  61. {
  62. return;
  63. }
  64. base.Dispose();
  65. foreach (UIType type in uis.Keys.ToArray())
  66. {
  67. UI ui;
  68. if (!uis.TryGetValue(type, out ui))
  69. {
  70. continue;
  71. }
  72. uis.Remove(type);
  73. ui.Dispose();
  74. }
  75. }
  76. public void Awake()
  77. {
  78. this.Root = GameObject.Find("Global/UI/");
  79. this.Load();
  80. }
  81. public void Load()
  82. {
  83. UiTypes = new Dictionary<UIType, IUIFactory>();
  84. Type[] types = DllHelper.GetHotfixTypes();
  85. foreach (Type type in types)
  86. {
  87. object[] attrs = type.GetCustomAttributes(typeof (UIFactoryAttribute), false);
  88. if (attrs.Length == 0)
  89. {
  90. continue;
  91. }
  92. UIFactoryAttribute attribute = attrs[0] as UIFactoryAttribute;
  93. if (UiTypes.ContainsKey((UIType)attribute.Type))
  94. {
  95. Log.Debug($"已经存在同类UI Factory: {attribute.Type}");
  96. throw new Exception($"已经存在同类UI Factory: {attribute.Type}");
  97. }
  98. #if ILRuntime
  99. IUIFactory iuiFactory = new IILUIFactoryMethod(type);
  100. #else
  101. IUIFactory iuiFactory = new IMonoUIFactoryMethod(type);
  102. #endif
  103. this.UiTypes.Add((UIType)attribute.Type, iuiFactory);
  104. }
  105. }
  106. public UI Create(UIType type)
  107. {
  108. try
  109. {
  110. UI ui = UiTypes[type].Create(GetOwner<Scene>(), type, Root);
  111. uis.Add(type, ui);
  112. // 设置canvas
  113. string cavasName = ui.GameObject.GetComponent<CanvasConfig>().CanvasName;
  114. ui.GameObject.transform.SetParent(this.Root.Get<GameObject>(cavasName).transform, false);
  115. return ui;
  116. }
  117. catch (Exception e)
  118. {
  119. throw new Exception($"{type} UI 错误: {e.ToStr()}");
  120. }
  121. }
  122. public void Add(UIType type, UI ui)
  123. {
  124. this.uis.Add(type, ui);
  125. }
  126. public void Remove(UIType type)
  127. {
  128. UI ui;
  129. if (!uis.TryGetValue(type, out ui))
  130. {
  131. return;
  132. }
  133. uis.Remove(type);
  134. ui.Dispose();
  135. }
  136. public void RemoveAll()
  137. {
  138. foreach (UIType type in this.uis.Keys.ToArray())
  139. {
  140. UI ui;
  141. if (!this.uis.TryGetValue(type, out ui))
  142. {
  143. continue;
  144. }
  145. this.uis.Remove(type);
  146. ui.Dispose();
  147. }
  148. }
  149. public UI Get(UIType type)
  150. {
  151. UI ui;
  152. this.uis.TryGetValue(type, out ui);
  153. return ui;
  154. }
  155. public List<UIType> GetUITypeList()
  156. {
  157. return new List<UIType>(this.uis.Keys);
  158. }
  159. }
  160. }