UIComponent.cs 3.8 KB

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