UI.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. namespace ET.Client
  4. {
  5. [FriendOf(typeof(UI))]
  6. public static class UISystem
  7. {
  8. [ObjectSystem]
  9. public class UIAwakeSystem : AwakeSystem<UI, string, GameObject>
  10. {
  11. protected override void Awake(UI self, string name, GameObject gameObject)
  12. {
  13. self.nameChildren.Clear();
  14. gameObject.layer = LayerMask.NameToLayer(LayerNames.UI);
  15. self.Name = name;
  16. self.GameObject = gameObject;
  17. }
  18. }
  19. [ObjectSystem]
  20. public class UIDestroySystem : DestroySystem<UI>
  21. {
  22. protected override void Destroy(UI self)
  23. {
  24. foreach (UI ui in self.nameChildren.Values)
  25. {
  26. ui.Dispose();
  27. }
  28. UnityEngine.Object.Destroy(self.GameObject);
  29. self.nameChildren.Clear();
  30. }
  31. }
  32. public static void SetAsFirstSibling(this UI self)
  33. {
  34. self.GameObject.transform.SetAsFirstSibling();
  35. }
  36. public static void Add(this UI self, UI ui)
  37. {
  38. self.nameChildren.Add(ui.Name, ui);
  39. }
  40. public static void Remove(this UI self, string name)
  41. {
  42. UI ui;
  43. if (!self.nameChildren.TryGetValue(name, out ui))
  44. {
  45. return;
  46. }
  47. self.nameChildren.Remove(name);
  48. ui.Dispose();
  49. }
  50. public static UI Get(this UI self, string name)
  51. {
  52. UI child;
  53. if (self.nameChildren.TryGetValue(name, out child))
  54. {
  55. return child;
  56. }
  57. GameObject childGameObject = self.GameObject.transform.Find(name)?.gameObject;
  58. if (childGameObject == null)
  59. {
  60. return null;
  61. }
  62. child = self.AddChild<UI, string, GameObject>(name, childGameObject);
  63. self.Add(child);
  64. return child;
  65. }
  66. }
  67. [ChildOf()]
  68. public sealed class UI: Entity, IAwake<string, GameObject>, IDestroy
  69. {
  70. public GameObject GameObject { get; set; }
  71. public string Name { get; set; }
  72. public Dictionary<string, UI> nameChildren = new Dictionary<string, UI>();
  73. }
  74. }