UIEventComponentSystem.cs 2.3 KB

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