303 lines
8.4 KiB
C#
303 lines
8.4 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.Threading.Tasks;
|
||
using DG.Tweening;
|
||
using UnityEngine;
|
||
using UnityEngine.UI;
|
||
#if HotUpdate
|
||
using YooAsset;
|
||
#endif
|
||
|
||
namespace Stary.Evo.UIFarme
|
||
{
|
||
public interface IBasePanel : IController
|
||
{
|
||
/// <summary>
|
||
/// UI信息
|
||
/// </summary>
|
||
string UIName { get; set; }
|
||
|
||
/// <summary>
|
||
/// 绑定这个面板的实例
|
||
/// </summary>
|
||
void Initialize(GameObject panelGo);
|
||
/// <summary>
|
||
/// 绑定这个面板的实例
|
||
/// </summary>
|
||
Task InitializeAsync(GameObject panelGo);
|
||
/// <summary>
|
||
/// 初始化面板管理器
|
||
/// </summary>
|
||
/// <param name="panelManager"></param>
|
||
void Initialize(IPanelSystem sysytem);
|
||
/// <summary>
|
||
/// 初始化面板管理器
|
||
/// </summary>
|
||
/// <param name="panelManager"></param>
|
||
Task InitializeAsync(IPanelSystem sysytem);
|
||
/// <summary>
|
||
/// 虚方法,UI进入时执行的操作,只会执行一次
|
||
/// </summary>
|
||
void OnEnter(Action complete = null);
|
||
|
||
/// <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(string panelName, string packageName);
|
||
|
||
/// <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 UIName { get; set; }
|
||
|
||
public abstract UITweenType TweenType { get; }
|
||
|
||
/// <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()
|
||
{
|
||
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 Task InitializeAsync(GameObject panelGo)
|
||
{
|
||
Initialize(panelGo);
|
||
return Task.CompletedTask;
|
||
}
|
||
|
||
public virtual void Initialize(IPanelSystem sysytem)
|
||
{
|
||
PanelSystem = sysytem;
|
||
}
|
||
|
||
public virtual Task InitializeAsync(IPanelSystem sysytem)
|
||
{
|
||
Initialize(sysytem);
|
||
return Task.CompletedTask;
|
||
}
|
||
|
||
|
||
public virtual void OnEnter(Action complete = null)
|
||
{
|
||
activePanel.SetActive(true);
|
||
canvasGroup.blocksRaycasts = true;
|
||
if (TweenType == UITweenType.None)
|
||
{
|
||
canvasGroup.alpha = 1f;
|
||
}
|
||
|
||
if (TweenType == UITweenType.Fade)
|
||
{
|
||
canvasGroup.DOFade(1f, 2f).OnComplete(() => { complete?.Invoke(); });
|
||
}
|
||
else if (TweenType == UITweenType.Yoyo)
|
||
{
|
||
canvasGroup.DOFade(1, 2f).SetLoops(2, LoopType.Yoyo).OnComplete(() => { complete?.Invoke(); });
|
||
}
|
||
else
|
||
{
|
||
canvasGroup.DOFade(1, 2f).SetLoops(-1).OnComplete(() => { complete?.Invoke(); });
|
||
}
|
||
}
|
||
|
||
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(string panelName, string packageName)
|
||
{
|
||
if (panelParent == null)
|
||
{
|
||
Debug.LogWarning($"UnityEvo:parent为空,{panelName}自动设置为root节点下,请检查是否正确!!!!!");
|
||
}
|
||
|
||
if (this.activePanel != null)
|
||
{
|
||
return activePanel.gameObject;
|
||
}
|
||
#if HotUpdate
|
||
AssetHandle handle = null;
|
||
if (packageName == null)
|
||
{
|
||
handle = YooAssets.LoadAssetAsync<GameObject>(panelName);
|
||
}
|
||
else
|
||
{
|
||
var package = YooAssets.TryGetPackage(packageName);
|
||
if (package == null)
|
||
{
|
||
handle = YooAssets.LoadAssetAsync<GameObject>(panelName);
|
||
}
|
||
else
|
||
{
|
||
handle = package.LoadAssetAsync<GameObject>(panelName);
|
||
}
|
||
}
|
||
|
||
await handle.Task;
|
||
|
||
activePanel = GameObject.Instantiate(handle.AssetObject as GameObject, panelParent);
|
||
#else
|
||
GameObject handle = Resources.Load<GameObject>(panelName);
|
||
activePanel = GameObject.Instantiate(handle, panelParent);
|
||
#endif
|
||
|
||
|
||
activePanel.name = this.GetType().Name;
|
||
|
||
|
||
if (!activePanel.GetComponentInChildren<Canvas>() && !activePanel.GetComponentInParent<Canvas>())
|
||
{
|
||
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;
|
||
}
|
||
|
||
/// <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();
|
||
}
|
||
|
||
public enum UITweenType
|
||
{
|
||
/// <summary>
|
||
/// 无动画
|
||
/// </summary>
|
||
None,
|
||
|
||
/// <summary>
|
||
/// 淡入淡出
|
||
/// </summary>
|
||
Fade,
|
||
|
||
/// <summary>
|
||
/// 循环播放
|
||
/// </summary>
|
||
Loop,
|
||
|
||
/// <summary>
|
||
/// 往返播放
|
||
/// </summary>
|
||
Yoyo,
|
||
}
|
||
}
|
||
} |