UI.cs 2.1 KB

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