UI.cs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. using System.Collections.Generic;
  2. using ETModel;
  3. using UnityEngine;
  4. namespace ETHotfix
  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 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(GameObject);
  39. children.Clear();
  40. }
  41. public void SetAsFirstSibling()
  42. {
  43. this.GameObject.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.GameObject.transform.Find(name)?.gameObject;
  68. if (childGameObject == null)
  69. {
  70. return null;
  71. }
  72. child = ComponentFactory.Create<UI, string, GameObject>(name, childGameObject);
  73. this.Add(child);
  74. return child;
  75. }
  76. }
  77. }