UIComponent.cs 3.0 KB

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