UIComponent.cs 3.1 KB

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