UI.cs 1.7 KB

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