UIComponent.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. if (!uis.TryGetValue(type, out UI ui))
  46. {
  47. continue;
  48. }
  49. uis.Remove(type);
  50. ui.Dispose();
  51. }
  52. }
  53. public 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. public void Load()
  60. {
  61. this.UiTypes = new Dictionary<int, IUIFactory>();
  62. Type[] types = DllHelper.GetHotfixTypes();
  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. if (!this.uis.TryGetValue(type, out UI ui))
  99. {
  100. return;
  101. }
  102. this.uis.Remove(type);
  103. ui.Dispose();
  104. }
  105. public void RemoveAll()
  106. {
  107. foreach (int type in this.uis.Keys.ToArray())
  108. {
  109. if (!this.uis.TryGetValue(type, out UI ui))
  110. {
  111. continue;
  112. }
  113. this.uis.Remove(type);
  114. ui.Dispose();
  115. }
  116. }
  117. public UI Get(int type)
  118. {
  119. this.uis.TryGetValue(type, out UI ui);
  120. return ui;
  121. }
  122. public List<int> GetUITypeList()
  123. {
  124. return new List<int>(this.uis.Keys);
  125. }
  126. }
  127. }