358 lines
10 KiB
C#
358 lines
10 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 OnExit(float delay = 0f);
|
||
|
||
void OnDestroy();
|
||
|
||
|
||
/// <summary>
|
||
/// 生成/获取一个UI对象
|
||
/// </summary>
|
||
/// <param name="type">ui信息</param>
|
||
/// <returns></returns>
|
||
Task<GameObject> CreatePanel(string packageName, string panelName);
|
||
|
||
/// <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; }
|
||
|
||
public abstract UIInstantiateType UIType { get; }
|
||
|
||
/// <summary>
|
||
/// 面板管理器
|
||
/// </summary>
|
||
protected IPanelSystem PanelSystem { get; private set; }
|
||
|
||
/// <summary>
|
||
/// 生成的父物体
|
||
/// </summary>
|
||
protected Transform panelParent { get; private set; }
|
||
|
||
|
||
/// <summary>
|
||
/// 缓存该面板所有的SpriteRenderer
|
||
/// </summary>
|
||
private Dictionary<string, SpriteRenderer> spriteRendererDict;
|
||
|
||
/// <summary>
|
||
/// 缓存该面板所有的Button,Toggle,Slider,InputField,Dropdown,ScroolBar
|
||
/// </summary>
|
||
private Dictionary<string, Selectable> selectableDict;
|
||
protected GameObject activePanel { get; private set; }
|
||
|
||
protected BasePanel()
|
||
{
|
||
spriteRendererDict = new Dictionary<string, SpriteRenderer>();
|
||
selectableDict = new Dictionary<string, Selectable>();
|
||
}
|
||
|
||
public virtual void Initialize(GameObject panelGo)
|
||
{
|
||
activePanel = panelGo;
|
||
if (UIType == UIInstantiateType.SpriteNormal)
|
||
{
|
||
SpriteRenderer[] spriteRenderers = activePanel.GetComponentsInChildren<SpriteRenderer>(true);
|
||
foreach (SpriteRenderer item in spriteRenderers)
|
||
{
|
||
spriteRendererDict[item.name] = item;
|
||
}
|
||
}else if(UIType == UIInstantiateType.UINormal){
|
||
Selectable[] selectables = activePanel.GetComponentsInChildren<Selectable>(true);
|
||
foreach (Selectable item in selectables)
|
||
{
|
||
selectableDict[item.name] = item;
|
||
}
|
||
var 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);
|
||
|
||
|
||
SetCanvasGroupTween(1f, complete);
|
||
}
|
||
|
||
|
||
public virtual void OnExit(float delay = 0f)
|
||
{
|
||
SetCanvasGroupTween(0f, () => { activePanel.SetActive(false); });
|
||
}
|
||
|
||
public virtual void OnDestroy()
|
||
{
|
||
DestoryUI();
|
||
}
|
||
|
||
|
||
public async Task<GameObject> CreatePanel(string packageName, string panelName)
|
||
{
|
||
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;
|
||
}
|
||
|
||
private void SetCanvasGroup(float alpha)
|
||
{
|
||
foreach (var spriteRenderer in spriteRendererDict)
|
||
{
|
||
Color c = spriteRenderer.Value.color;
|
||
c.a = alpha;
|
||
spriteRenderer.Value.color = c;
|
||
}
|
||
}
|
||
|
||
private void SetCanvasGroupTween(float alpha, Action complete = null)
|
||
{
|
||
if (UIType == UIInstantiateType.SpriteNormal)
|
||
{
|
||
// 创建一个新的序列
|
||
Sequence sequence = DOTween.Sequence();
|
||
|
||
|
||
foreach (var spriteRenderer in spriteRendererDict)
|
||
{
|
||
if (TweenType == UITweenType.None)
|
||
{
|
||
Color c = spriteRenderer.Value.color;
|
||
c.a = alpha;
|
||
spriteRenderer.Value.color = c;
|
||
}
|
||
else if (TweenType == UITweenType.Fade)
|
||
{
|
||
sequence.Join(spriteRenderer.Value.DOFade(alpha, 1f));
|
||
}
|
||
else if (TweenType == UITweenType.Yoyo)
|
||
{
|
||
sequence.Join(spriteRenderer.Value.DOFade(alpha, 1f)
|
||
.SetLoops(2, LoopType.Yoyo));
|
||
}
|
||
}
|
||
|
||
if (TweenType == UITweenType.None)
|
||
{
|
||
complete?.Invoke();
|
||
}
|
||
else
|
||
{
|
||
sequence.OnComplete(() => complete?.Invoke());
|
||
}
|
||
}
|
||
else if (UIType == UIInstantiateType.UINormal)
|
||
{
|
||
activePanel.SetActive(true);
|
||
var canvasGroup = activePanel.GetComponent<CanvasGroup>();
|
||
canvasGroup.blocksRaycasts = true;
|
||
if (TweenType == UITweenType.None)
|
||
{
|
||
canvasGroup.alpha = alpha;
|
||
}
|
||
|
||
if (TweenType == UITweenType.Fade)
|
||
{
|
||
canvasGroup.DOFade(alpha, 1f).OnComplete(() =>
|
||
{
|
||
complete?.Invoke();
|
||
canvasGroup.blocksRaycasts = false;
|
||
});
|
||
}
|
||
else if (TweenType == UITweenType.Yoyo)
|
||
{
|
||
canvasGroup.DOFade(alpha, 1f).SetLoops(2, LoopType.Yoyo).OnComplete(() => { complete?.Invoke(); });
|
||
}
|
||
else
|
||
{
|
||
canvasGroup.DOFade(alpha, 1f).SetLoops(-1).OnComplete(() => { complete?.Invoke(); });
|
||
}
|
||
}
|
||
}
|
||
|
||
public IArchitecture GetArchitecture()
|
||
{
|
||
return PanelSystem.GetArchitecture();
|
||
}
|
||
|
||
public enum UITweenType
|
||
{
|
||
/// <summary>
|
||
/// 无动画
|
||
/// </summary>
|
||
None,
|
||
|
||
/// <summary>
|
||
/// 淡入淡出
|
||
/// </summary>
|
||
Fade,
|
||
|
||
/// <summary>
|
||
/// 循环播放
|
||
/// </summary>
|
||
Loop,
|
||
|
||
/// <summary>
|
||
/// 往返播放
|
||
/// </summary>
|
||
Yoyo,
|
||
}
|
||
|
||
public enum UIInstantiateType
|
||
{
|
||
/// <summary>
|
||
/// 普通面板
|
||
/// </summary>
|
||
SpriteNormal,
|
||
|
||
/// <summary>
|
||
/// UI普通面板
|
||
/// </summary>
|
||
UINormal
|
||
}
|
||
}
|
||
} |