using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; namespace TFramework { public interface IPanelData:IReference { public void Recycle(); } public class PanelData : IPanelData { public void Reset() { } public void Recycle()=> Main.GetMagr().Recycle(this); } public class BasePanel { /// /// UI实体 /// public GameObject UIEntity { get; set; } protected Dictionary uiComponents; protected Dictionary ui2gam; public virtual bool StickyTopic { get; protected set; } = false; public bool IsOpen { get; protected set; } /// /// 初始化 UI实体被创建时执行 /// public virtual void OnInit() { uiComponents = new Dictionary(); ui2gam = new Dictionary(); GetAllUIBehaviour(); } /// /// 界面显示时 /// public virtual void OnEnter(IPanelData args = null) { IsOpen = true; } /// /// 界面暂停 置于其它界面之下 /// public virtual void OnPause(IPanelData args = null) { } /// /// 界面继续 /// public virtual void OnResume(IPanelData args = null) { } /// /// 退出界面 界面关闭 /// public virtual void OnExit(IPanelData args = null,bool isDestroy=false) { IsOpen = false; } /// /// 获取所有UIBehaviour /// private void GetAllUIBehaviour() { foreach (var item in UIEntity.GetComponentsInChildren(true)) { if (uiComponents.ContainsKey(item.ID)) throw new UnityException($"在同一个Panel下发现相同的UIBehaviourID:{item.ID}"); else { uiComponents.Add(item.ID, item); ui2gam.Add(item.gameObject, item); } } } /// /// 获取UIBehaviour /// /// /// protected UIBehaviour GetUIBehaviour(string uiId) { if (uiComponents.ContainsKey(uiId)) return uiComponents[uiId]; else { Log.Error($"{UIEntity.name}下未找到ID为{uiId}的UIBehaviour!"); return null; } } protected T GetComponentInUIID(string uiId) where T: Component { return GetUIBehaviour(uiId)?.GetComponent(); } protected Component GetComponentInUIID(Type type, string uiId) { return GetUIBehaviour(uiId)?.GetComponent(type); } protected T GetUseRes(GameObject ui,string resName) where T : UnityEngine.Object { if (ui2gam.ContainsKey(ui)) return ui2gam[ui].GetRes(resName) as T; Debug.Log($"对象{ui.name}的资源列表中没有名字为{resName}的资源"); return null; } /// /// 切换语言 /// internal void ChangedWord() { foreach (var item in uiComponents.Values) { Text text = item.GetComponent(); if (!text) continue; string str = Main.GetMagr()?.GetLanguageText(text.text); if (string.IsNullOrEmpty(str)) continue; text.text = str; } } } }