UI.cs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. [HideInHierarchy]
  13. public sealed class UI: Entity
  14. {
  15. public GameObject GameObject;
  16. public string Name { get; private set; }
  17. public Dictionary<string, UI> children = new Dictionary<string, UI>();
  18. public void Awake(string name, GameObject gameObject)
  19. {
  20. this.children.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.children.Values)
  34. {
  35. ui.Dispose();
  36. }
  37. UnityEngine.Object.Destroy(this.ViewGO);
  38. children.Clear();
  39. }
  40. public void SetAsFirstSibling()
  41. {
  42. this.ViewGO.transform.SetAsFirstSibling();
  43. }
  44. public void Add(UI ui)
  45. {
  46. this.children.Add(ui.Name, ui);
  47. ui.Parent = this;
  48. }
  49. public void Remove(string name)
  50. {
  51. UI ui;
  52. if (!this.children.TryGetValue(name, out ui))
  53. {
  54. return;
  55. }
  56. this.children.Remove(name);
  57. ui.Dispose();
  58. }
  59. public UI Get(string name)
  60. {
  61. UI child;
  62. if (this.children.TryGetValue(name, out child))
  63. {
  64. return child;
  65. }
  66. GameObject childGameObject = this.ViewGO.transform.Find(name)?.gameObject;
  67. if (childGameObject == null)
  68. {
  69. return null;
  70. }
  71. child = EntityFactory.Create<UI, string, GameObject>(this.Domain, name, childGameObject);
  72. this.Add(child);
  73. return child;
  74. }
  75. }
  76. }