UI.cs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. namespace Hotfix
  4. {
  5. [Model.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. }
  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. }