UI.cs 1.7 KB

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