UI.cs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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.layer = LayerMask.NameToLayer(LayerNames.UI);
  22. this.Name = name;
  23. this.GameObject = gameObject;
  24. }
  25. public override void Dispose()
  26. {
  27. if (this.IsDisposed)
  28. {
  29. return;
  30. }
  31. base.Dispose();
  32. foreach (UI ui in this.nameChildren.Values)
  33. {
  34. ui.Dispose();
  35. }
  36. UnityEngine.Object.Destroy(this.GameObject);
  37. this.nameChildren.Clear();
  38. }
  39. public void SetAsFirstSibling()
  40. {
  41. this.GameObject.transform.SetAsFirstSibling();
  42. }
  43. public void Add(UI ui)
  44. {
  45. this.nameChildren.Add(ui.Name, ui);
  46. }
  47. public void Remove(string name)
  48. {
  49. UI ui;
  50. if (!this.nameChildren.TryGetValue(name, out ui))
  51. {
  52. return;
  53. }
  54. this.nameChildren.Remove(name);
  55. ui.Dispose();
  56. }
  57. public UI Get(string name)
  58. {
  59. UI child;
  60. if (this.nameChildren.TryGetValue(name, out child))
  61. {
  62. return child;
  63. }
  64. GameObject childGameObject = this.GameObject.transform.Find(name)?.gameObject;
  65. if (childGameObject == null)
  66. {
  67. return null;
  68. }
  69. child = this.AddChild<UI, string, GameObject>(name, childGameObject);
  70. this.Add(child);
  71. return child;
  72. }
  73. }
  74. }