UIComponent.cs 2.9 KB

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