UI.cs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. }
  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. }
  57. public void Remove(string name)
  58. {
  59. UI ui;
  60. if (!this.children.TryGetValue(name, out ui))
  61. {
  62. return;
  63. }
  64. this.children.Remove(name);
  65. ui.Dispose();
  66. }
  67. public UI Get(string name)
  68. {
  69. UI child;
  70. if (this.children.TryGetValue(name, out child))
  71. {
  72. return child;
  73. }
  74. GameObject childGameObject = this.GameObject.transform.Find(name)?.gameObject;
  75. if (childGameObject == null)
  76. {
  77. return null;
  78. }
  79. child = EntityFactory.Create<UI, Scene, UI, GameObject>(this.Scene, this, childGameObject);
  80. this.Add(child);
  81. return child;
  82. }
  83. }
  84. }