UI.cs 1.8 KB

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