UI.cs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. namespace Hotfix
  4. {
  5. [Model.ObjectSystem]
  6. public class UiSystem : 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. }
  43. public void SetAsFirstSibling()
  44. {
  45. this.GameObject.transform.SetAsFirstSibling();
  46. }
  47. public void Add(UI ui)
  48. {
  49. this.children.Add(ui.Name, ui);
  50. ui.Parent = this;
  51. }
  52. public void Remove(string name)
  53. {
  54. UI ui;
  55. if (!this.children.TryGetValue(name, out ui))
  56. {
  57. return;
  58. }
  59. this.children.Remove(name);
  60. ui.Dispose();
  61. }
  62. public UI Get(string name)
  63. {
  64. UI child;
  65. if (this.children.TryGetValue(name, out child))
  66. {
  67. return child;
  68. }
  69. GameObject childGameObject = this.GameObject.transform.Find(name)?.gameObject;
  70. if (childGameObject == null)
  71. {
  72. return null;
  73. }
  74. child = ComponentFactory.Create<UI, GameObject>(childGameObject);
  75. this.Add(child);
  76. return child;
  77. }
  78. }
  79. }