Unity 框架

BasePanel.cs 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6. namespace TFramework
  7. {
  8. public interface IPanelData:IReference { }
  9. public class PanelData : IPanelData
  10. {
  11. public void Reset()
  12. {
  13. }
  14. }
  15. public class BasePanel
  16. {
  17. /// <summary>
  18. /// UI实体
  19. /// </summary>
  20. public GameObject UIEntity { get; set; }
  21. protected Dictionary<string, UIBehaviour> uiComponents;
  22. public virtual bool StickyTopic { get; protected set; } = false;
  23. public bool IsOpen { get; protected set; }
  24. /// <summary>
  25. /// 初始化 UI实体被创建时执行
  26. /// </summary>
  27. public virtual void OnInit()
  28. {
  29. uiComponents = new Dictionary<string, UIBehaviour>();
  30. GetAllUIBehaviour();
  31. }
  32. /// <summary>
  33. /// 界面显示时
  34. /// </summary>
  35. public virtual void OnEnter(IPanelData args = null)
  36. {
  37. IsOpen = true;
  38. }
  39. /// <summary>
  40. /// 界面暂停 置于其它界面之下
  41. /// </summary>
  42. public virtual void OnPause(IPanelData args = null)
  43. {
  44. }
  45. /// <summary>
  46. /// 界面继续
  47. /// </summary>
  48. public virtual void OnResume(IPanelData args = null)
  49. {
  50. }
  51. /// <summary>
  52. /// 退出界面 界面关闭
  53. /// </summary>
  54. public virtual void OnExit(IPanelData args = null)
  55. {
  56. IsOpen = false;
  57. }
  58. /// <summary>
  59. /// 获取所有UIBehaviour
  60. /// </summary>
  61. private void GetAllUIBehaviour()
  62. {
  63. foreach (var item in UIEntity.GetComponentsInChildren<UIBehaviour>(true))
  64. {
  65. if (uiComponents.ContainsKey(item.ID))
  66. throw new UnityException($"在同一个Panel下发现相同的UIBehaviourID:{item.ID}");
  67. else
  68. uiComponents.Add(item.ID, item);
  69. }
  70. }
  71. /// <summary>
  72. /// 获取UIBehaviour
  73. /// </summary>
  74. /// <param name="uiId"></param>
  75. /// <returns></returns>
  76. protected UIBehaviour GetUIBehaviour(string uiId)
  77. {
  78. if (uiComponents.ContainsKey(uiId))
  79. return uiComponents[uiId];
  80. else
  81. {
  82. Log.Error($"{UIEntity.name}下未找到ID为{uiId}的UIBehaviour!");
  83. return null;
  84. }
  85. }
  86. protected T GetComponentInUIID<T>(string uiId) where T: Component
  87. {
  88. return GetUIBehaviour(uiId)?.GetComponent<T>();
  89. }
  90. protected Component GetComponentInUIID(Type type, string uiId)
  91. {
  92. return GetUIBehaviour(uiId)?.GetComponent(type);
  93. }
  94. /// <summary>
  95. /// 切换语言
  96. /// </summary>
  97. internal void ChangedWord()
  98. {
  99. foreach (var item in uiComponents.Values)
  100. {
  101. Text text = item.GetComponent<Text>();
  102. if (!text) continue;
  103. string str = Main.GetMagr<LanguageManager>()?.GetLanguageText(text.text);
  104. if (string.IsNullOrEmpty(str)) continue;
  105. text.text = str;
  106. }
  107. }
  108. }
  109. }