UI.cs 1.6 KB

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