UIEventComponentSystem.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace ET
  5. {
  6. public class UIEventComponentAwakeSystem : AwakeSystem<UIEventComponent>
  7. {
  8. public override void Awake(UIEventComponent self)
  9. {
  10. UIEventComponent.Instance = self;
  11. GameObject uiRoot = GameObject.Find("/Global/UI");
  12. ReferenceCollector referenceCollector = uiRoot.GetComponent<ReferenceCollector>();
  13. self.UILayers.Add((int)UILayer.Hidden, referenceCollector.Get<GameObject>(UILayer.Hidden.ToString()).transform);
  14. self.UILayers.Add((int)UILayer.Low, referenceCollector.Get<GameObject>(UILayer.Low.ToString()).transform);
  15. self.UILayers.Add((int)UILayer.Mid, referenceCollector.Get<GameObject>(UILayer.Mid.ToString()).transform);
  16. self.UILayers.Add((int)UILayer.High, referenceCollector.Get<GameObject>(UILayer.High.ToString()).transform);
  17. var uiEvents = Game.EventSystem.GetTypes(typeof (UIEventAttribute));
  18. foreach (Type type in uiEvents)
  19. {
  20. object[] attrs = type.GetCustomAttributes(typeof(UIEventAttribute), false);
  21. if (attrs.Length == 0)
  22. {
  23. continue;
  24. }
  25. UIEventAttribute uiEventAttribute = attrs[0] as UIEventAttribute;
  26. AUIEvent aUIEvent = Activator.CreateInstance(type) as AUIEvent;
  27. self.UIEvents.Add(uiEventAttribute.UIType, aUIEvent);
  28. }
  29. }
  30. }
  31. /// <summary>
  32. /// 管理所有UI GameObject 以及UI事件
  33. /// </summary>
  34. public static class UIEventComponentSystem
  35. {
  36. public static async ETTask<UI> OnCreate(this UIEventComponent self, UIComponent uiComponent, string uiType)
  37. {
  38. try
  39. {
  40. UI ui = await self.UIEvents[uiType].OnCreate(uiComponent);
  41. UILayer uiLayer = ui.GameObject.GetComponent<UILayerScript>().UILayer;
  42. ui.GameObject.transform.SetParent(self.UILayers[(int)uiLayer]);
  43. return ui;
  44. }
  45. catch (Exception e)
  46. {
  47. throw new Exception($"on create ui error: {uiType}", e);
  48. }
  49. }
  50. public static void OnRemove(this UIEventComponent self, UIComponent uiComponent, string uiType)
  51. {
  52. try
  53. {
  54. self.UIEvents[uiType].OnRemove(uiComponent);
  55. }
  56. catch (Exception e)
  57. {
  58. throw new Exception($"on remove ui error: {uiType}", e);
  59. }
  60. }
  61. }
  62. }