UIEventComponentSystem.cs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. var uiEvents = Game.EventSystem.GetTypes(typeof (UIEventAttribute));
  12. foreach (Type type in uiEvents)
  13. {
  14. object[] attrs = type.GetCustomAttributes(typeof(UIEventAttribute), false);
  15. if (attrs.Length == 0)
  16. {
  17. continue;
  18. }
  19. UIEventAttribute uiEventAttribute = attrs[0] as UIEventAttribute;
  20. AUIEvent aUIEvent = Activator.CreateInstance(type) as AUIEvent;
  21. self.UIEvents.Add(uiEventAttribute.UIType, aUIEvent);
  22. }
  23. }
  24. }
  25. /// <summary>
  26. /// 管理所有UI GameObject 以及UI事件
  27. /// </summary>
  28. public static class UIEventComponentSystem
  29. {
  30. public static async ETTask<UI> OnCreate(this UIEventComponent self, UIComponent uiComponent, string uiType)
  31. {
  32. try
  33. {
  34. UI ui = await self.UIEvents[uiType].OnCreate(uiComponent);
  35. return ui;
  36. }
  37. catch (Exception e)
  38. {
  39. throw new Exception($"on create ui error: {uiType}", e);
  40. }
  41. }
  42. public static void OnRemove(this UIEventComponent self, UIComponent uiComponent, string uiType)
  43. {
  44. try
  45. {
  46. self.UIEvents[uiType].OnRemove(uiComponent);
  47. }
  48. catch (Exception e)
  49. {
  50. throw new Exception($"on remove ui error: {uiType}", e);
  51. }
  52. }
  53. }
  54. }