Unity 框架

BasePanel.cs 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. {
  10. public void Recycle();
  11. }
  12. public class PanelData : IPanelData
  13. {
  14. public void Reset()
  15. {
  16. }
  17. public void Recycle()=> Main.GetMagr<ReferencePoolManager>().Recycle(this);
  18. }
  19. public class BasePanel
  20. {
  21. /// <summary>
  22. /// UI实体
  23. /// </summary>
  24. public GameObject UIEntity { get; set; }
  25. protected Dictionary<string, UIBehaviour> uiComponents;
  26. protected Dictionary<GameObject, UIBehaviour> ui2gam;
  27. public virtual bool StickyTopic { get; protected set; } = false;
  28. public bool IsOpen { get; protected set; }
  29. /// <summary>
  30. /// 初始化 UI实体被创建时执行
  31. /// </summary>
  32. public virtual void OnInit()
  33. {
  34. uiComponents = new Dictionary<string, UIBehaviour>();
  35. ui2gam = new Dictionary<GameObject, UIBehaviour>();
  36. GetAllUIBehaviour();
  37. }
  38. /// <summary>
  39. /// 界面显示时
  40. /// </summary>
  41. public virtual void OnEnter(IPanelData args = null)
  42. {
  43. IsOpen = true;
  44. }
  45. /// <summary>
  46. /// 界面暂停 置于其它界面之下
  47. /// </summary>
  48. public virtual void OnPause(IPanelData args = null)
  49. {
  50. }
  51. /// <summary>
  52. /// 界面继续
  53. /// </summary>
  54. public virtual void OnResume(IPanelData args = null)
  55. {
  56. }
  57. /// <summary>
  58. /// 退出界面 界面关闭
  59. /// </summary>
  60. public virtual void OnExit(IPanelData args = null,bool isDestroy=false)
  61. {
  62. IsOpen = false;
  63. }
  64. /// <summary>
  65. /// 获取所有UIBehaviour
  66. /// </summary>
  67. private void GetAllUIBehaviour()
  68. {
  69. foreach (var item in UIEntity.GetComponentsInChildren<UIBehaviour>(true))
  70. {
  71. if (uiComponents.ContainsKey(item.ID))
  72. throw new UnityException($"在同一个Panel下发现相同的UIBehaviourID:{item.ID}");
  73. else
  74. {
  75. uiComponents.Add(item.ID, item);
  76. ui2gam.Add(item.gameObject, item);
  77. }
  78. }
  79. }
  80. /// <summary>
  81. /// 获取UIBehaviour
  82. /// </summary>
  83. /// <param name="uiId"></param>
  84. /// <returns></returns>
  85. protected UIBehaviour GetUIBehaviour(string uiId)
  86. {
  87. if (uiComponents.ContainsKey(uiId))
  88. return uiComponents[uiId];
  89. else
  90. {
  91. Log.Error($"{UIEntity.name}下未找到ID为{uiId}的UIBehaviour!");
  92. return null;
  93. }
  94. }
  95. protected T GetComponentInUIID<T>(string uiId) where T: Component
  96. {
  97. return GetUIBehaviour(uiId)?.GetComponent<T>();
  98. }
  99. protected Component GetComponentInUIID(Type type, string uiId)
  100. {
  101. return GetUIBehaviour(uiId)?.GetComponent(type);
  102. }
  103. protected T GetUseRes<T>(GameObject ui,string resName) where T : UnityEngine.Object
  104. {
  105. if (ui2gam.ContainsKey(ui))
  106. return ui2gam[ui].GetRes(resName) as T;
  107. Debug.Log($"对象{ui.name}的资源列表中没有名字为{resName}的资源");
  108. return null;
  109. }
  110. /// <summary>
  111. /// 切换语言
  112. /// </summary>
  113. internal void ChangedWord()
  114. {
  115. foreach (var item in uiComponents.Values)
  116. {
  117. Text text = item.GetComponent<Text>();
  118. if (!text) continue;
  119. string str = Main.GetMagr<LanguageManager>()?.GetLanguageText(text.text);
  120. if (string.IsNullOrEmpty(str)) continue;
  121. text.text = str;
  122. }
  123. }
  124. }
  125. }