UI.cs 1.7 KB

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