using System.Collections.Generic; using System.Threading.Tasks; using DG.Tweening; using UnityEngine; using UnityEngine.UI; using YooAsset; namespace Stary.Evo.UIFarme { public interface IBasePanel : IController { /// /// 绑定这个面板的实例 /// void Initialize(GameObject panelGo); /// /// 初始化面板管理器 /// /// void Initialize(IPanelSystem sysytem); /// /// 虚方法,UI进入时执行的操作,只会执行一次 /// void OnEnter(); /// /// 虚方法,UI暂停时执行的操作,只会执行一次 /// void OnPause(); /// /// 虚方法,UI继续时执行的操作,只会执行一次 /// void OnResume(); /// /// 虚方法,UI退出时执行的操作,只会执行一次 /// void OnExit(float delay = 0f); void OnDestroy(); /// /// 生成/获取一个UI对象 /// /// ui信息 /// Task CreatePanel(string panelName, string packageName); /// /// 销毁一个Ui对象 /// /// void DestoryUI(); void SetPanelParent(Transform parent); } /// ///所有UI面板的父类,包含UI面板的状态信息 /// public abstract class BasePanel : IBasePanel { /// /// UI信息 /// public string UIPath { get; set; } /// /// 面板管理器 /// protected IPanelSystem PanelSystem { get; private set; } /// /// 生成的父物体 /// protected Transform panelParent { get; private set; } /// /// 缓存该面板所有的Button,Toggle,Slider,InputField,Dropdown,ScroolBar /// private Dictionary selectableDict; protected CanvasGroup canvasGroup { get; private set; } protected GameObject activePanel { get; private set; } protected BasePanel() { selectableDict = new Dictionary(); } public virtual void Initialize(GameObject panelGo) { activePanel = panelGo; Selectable[] selectables = activePanel.GetComponentsInChildren(true); foreach (Selectable item in selectables) { selectableDict[item.name] = item; } canvasGroup = UITool.GetOrAddComponent(activePanel); canvasGroup.alpha = 0f; } public virtual void Initialize(IPanelSystem sysytem) { PanelSystem = sysytem; } public virtual void OnEnter() { activePanel.SetActive(true); canvasGroup.blocksRaycasts = true; canvasGroup.DOFade(1f, 1f); } public virtual void OnPause() { canvasGroup.blocksRaycasts = false; } public virtual void OnResume() { canvasGroup.blocksRaycasts = true; } public virtual void OnExit(float delay = 0f) { canvasGroup.DOFade(0f, delay).OnComplete(() => { activePanel.SetActive(false); canvasGroup.blocksRaycasts = false; }); } public virtual void OnDestroy() { DestoryUI(); } public async Task CreatePanel(string panelName, string packageName) { if (panelParent == null) { Debug.LogError($"UnityEvo:parent为空,{panelName}无法创建,进程已中断,请检查!!!!!"); return null; } if (this.activePanel != null) { return activePanel.gameObject; } AssetHandle handle = null; if (packageName == null) { handle = YooAssets.LoadAssetAsync(panelName); } else { var package= YooAssets.TryGetPackage(packageName); if (package == null) { handle = YooAssets.LoadAssetAsync(panelName); } else { handle = package.LoadAssetAsync(panelName); } } await handle.Task; activePanel = GameObject.Instantiate(handle.AssetObject as GameObject, panelParent); activePanel.name = this.GetType().Name; if (!activePanel.GetComponent()) { Debug.LogError($"UnityEvo:panelParent上不存在Canvas组件,{panelName}无法正常运行,进程已中断,请检查!!!!!"); return null; } return activePanel; } public void DestoryUI() { if (activePanel != null) { GameObject.Destroy(activePanel); PanelSystem.Get_Dic().Remove(this.GetType().Name); } } public void SetPanelParent(Transform parent) { panelParent = parent; } /// /// 获取组件:Button,Toggle,Slider,InputField,Dropdown,ScroolBar /// /// /// /// protected T GetComp(string name) where T : Selectable { selectableDict.TryGetValue(name, out Selectable t); if (t == null) { Debug.LogError($"{this.GetType()}---{name}:在selectableDict不存在"); } return t as T; } public IArchitecture GetArchitecture() { return PanelSystem.GetArchitecture(); } } }