UI.cs 1.8 KB

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