UI.cs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. namespace ET
  4. {
  5. public class UIAwakeSystem : AwakeSystem<UI, string, GameObject>
  6. {
  7. public override void Awake(UI self, string name, GameObject gameObject)
  8. {
  9. self.Awake(name, gameObject);
  10. }
  11. }
  12. public sealed class UI: Entity
  13. {
  14. public GameObject GameObject;
  15. public string Name { get; private set; }
  16. public Dictionary<string, UI> nameChildren = new Dictionary<string, UI>();
  17. public void Awake(string name, GameObject gameObject)
  18. {
  19. this.nameChildren.Clear();
  20. gameObject.AddComponent<ComponentView>().Component = this;
  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. ui.Parent = this;
  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 = EntityFactory.Create<UI, string, GameObject>(this.Domain, name, childGameObject);
  71. this.Add(child);
  72. return child;
  73. }
  74. }
  75. }