UIEventComponentSystem.cs 2.0 KB

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