增加06 UI框架
This commit is contained in:
218
Assets/06.UIFarme/RunTime/Base/BasePanel.cs
Normal file
218
Assets/06.UIFarme/RunTime/Base/BasePanel.cs
Normal file
@@ -0,0 +1,218 @@
|
||||
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
|
||||
{
|
||||
/// <summary>
|
||||
/// UI信息
|
||||
/// </summary>
|
||||
string UIPath { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 绑定这个面板的实例
|
||||
/// </summary>
|
||||
void Initialize(GameObject panelGo);
|
||||
|
||||
/// <summary>
|
||||
/// 初始化面板管理器
|
||||
/// </summary>
|
||||
/// <param name="panelManager"></param>
|
||||
void Initialize(IPanelSystem sysytem);
|
||||
|
||||
/// <summary>
|
||||
/// 虚方法,UI进入时执行的操作,只会执行一次
|
||||
/// </summary>
|
||||
void OnEnter();
|
||||
|
||||
/// <summary>
|
||||
/// 虚方法,UI暂停时执行的操作,只会执行一次
|
||||
/// </summary>
|
||||
void OnPause();
|
||||
|
||||
/// <summary>
|
||||
/// 虚方法,UI继续时执行的操作,只会执行一次
|
||||
/// </summary>
|
||||
void OnResume();
|
||||
|
||||
/// <summary>
|
||||
/// 虚方法,UI退出时执行的操作,只会执行一次
|
||||
/// </summary>
|
||||
void OnExit(float delay = 0f);
|
||||
|
||||
void OnDestroy();
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 生成/获取一个UI对象
|
||||
/// </summary>
|
||||
/// <param name="type">ui信息</param>
|
||||
/// <returns></returns>
|
||||
Task<GameObject> CreatePanel();
|
||||
|
||||
/// <summary>
|
||||
/// 销毁一个Ui对象
|
||||
/// </summary>
|
||||
/// <param name="type"></param>
|
||||
void DestoryUI();
|
||||
|
||||
void SetPanelParent(Transform parent);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
///所有UI面板的父类,包含UI面板的状态信息
|
||||
/// </summary>
|
||||
public abstract class BasePanel : IBasePanel
|
||||
{
|
||||
/// <summary>
|
||||
/// UI信息
|
||||
/// </summary>
|
||||
public string UIPath { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 面板管理器
|
||||
/// </summary>
|
||||
protected IPanelSystem PanelSystem { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// 生成的父物体
|
||||
/// </summary>
|
||||
protected Transform panelParent{get; private set;}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 缓存该面板所有的Button,Toggle,Slider,InputField,Dropdown,ScroolBar
|
||||
/// </summary>
|
||||
private Dictionary<string, Selectable> selectableDict;
|
||||
|
||||
protected CanvasGroup canvasGroup{get;private set;}
|
||||
|
||||
protected GameObject activePanel{get;private set;}
|
||||
|
||||
protected BasePanel(string uiPath)
|
||||
{
|
||||
UIPath = uiPath;
|
||||
selectableDict = new Dictionary<string, Selectable>();
|
||||
}
|
||||
|
||||
public virtual void Initialize(GameObject panelGo)
|
||||
{
|
||||
activePanel = panelGo;
|
||||
Selectable[] selectables = activePanel.GetComponentsInChildren<Selectable>(true);
|
||||
foreach (Selectable item in selectables)
|
||||
{
|
||||
selectableDict[item.name] = item;
|
||||
}
|
||||
|
||||
canvasGroup = UITool.GetOrAddComponent<CanvasGroup>(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<GameObject> CreatePanel()
|
||||
{
|
||||
if (panelParent == null)
|
||||
{
|
||||
Debug.LogError($"UnityEvo:parent为空,{this.UIPath}无法创建,进程已中断,请检查!!!!!");
|
||||
return null;
|
||||
}
|
||||
|
||||
if (!panelParent.GetComponent<Canvas>())
|
||||
{
|
||||
Debug.LogError($"UnityEvo:panelParent上不存在Canvas组件,{this.UIPath}无法创建,进程已中断,请检查!!!!!");
|
||||
return null;
|
||||
}
|
||||
|
||||
if (this.activePanel != null)
|
||||
{
|
||||
return activePanel.gameObject;
|
||||
}
|
||||
|
||||
var handle= YooAssets.LoadAssetAsync<GameObject>(UIPath);
|
||||
await handle.Task;
|
||||
|
||||
activePanel = GameObject.Instantiate(handle.AssetObject as GameObject,panelParent);
|
||||
activePanel.name = this.GetType().Name;
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取组件:Button,Toggle,Slider,InputField,Dropdown,ScroolBar
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="name"></param>
|
||||
/// <returns></returns>
|
||||
protected T GetComp<T>(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();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user