123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- namespace TFramework
- {
- public interface IPanelData:IReference { }
- public class PanelData : IPanelData
- {
- public void Reset()
- {
-
- }
- }
- public class BasePanel
- {
- /// <summary>
- /// UI实体
- /// </summary>
- public GameObject UIEntity { get; set; }
- protected Dictionary<string, UIBehaviour> uiComponents;
- public virtual bool StickyTopic { get; protected set; } = false;
- public bool IsOpen { get; protected set; }
- /// <summary>
- /// 初始化 UI实体被创建时执行
- /// </summary>
- public virtual void OnInit()
- {
- uiComponents = new Dictionary<string, UIBehaviour>();
- GetAllUIBehaviour();
- }
- /// <summary>
- /// 界面显示时
- /// </summary>
- public virtual void OnEnter(IPanelData args = null)
- {
- IsOpen = true;
- }
- /// <summary>
- /// 界面暂停 置于其它界面之下
- /// </summary>
- public virtual void OnPause(IPanelData args = null)
- {
- }
- /// <summary>
- /// 界面继续
- /// </summary>
- public virtual void OnResume(IPanelData args = null)
- {
- }
- /// <summary>
- /// 退出界面 界面关闭
- /// </summary>
- public virtual void OnExit(IPanelData args = null)
- {
- IsOpen = false;
- }
- /// <summary>
- /// 获取所有UIBehaviour
- /// </summary>
- private void GetAllUIBehaviour()
- {
- foreach (var item in UIEntity.GetComponentsInChildren<UIBehaviour>(true))
- {
- if (uiComponents.ContainsKey(item.ID))
- throw new UnityException($"在同一个Panel下发现相同的UIBehaviourID:{item.ID}");
- else
- uiComponents.Add(item.ID, item);
- }
- }
- /// <summary>
- /// 获取UIBehaviour
- /// </summary>
- /// <param name="uiId"></param>
- /// <returns></returns>
- 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<T>(string uiId) where T: Component
- {
- return GetUIBehaviour(uiId)?.GetComponent<T>();
- }
- protected Component GetComponentInUIID(Type type, string uiId)
- {
- return GetUIBehaviour(uiId)?.GetComponent(type);
- }
- /// <summary>
- /// 切换语言
- /// </summary>
- internal void ChangedWord()
- {
- foreach (var item in uiComponents.Values)
- {
- Text text = item.GetComponent<Text>();
- if (!text) continue;
- string str = Main.GetMagr<LanguageManager>()?.GetLanguageText(text.text);
- if (string.IsNullOrEmpty(str)) continue;
- text.text = str;
- }
- }
- }
- }
|