Files
plugin-library/Assets/00.StaryEvoTools/Runtime/Tools/PanelSystem/Base/BasePanel.cs

341 lines
9.7 KiB
C#
Raw Normal View History

2025-10-14 14:29:08 +08:00
using System;
using System.Collections.Generic;
2025-04-10 18:15:01 +08:00
using System.Threading.Tasks;
using DG.Tweening;
using UnityEngine;
using UnityEngine.UI;
2025-06-25 15:51:50 +08:00
#if HotUpdate
2025-04-10 18:15:01 +08:00
using YooAsset;
2025-06-25 15:51:50 +08:00
#endif
2025-10-14 14:29:08 +08:00
2025-04-10 18:15:01 +08:00
namespace Stary.Evo.UIFarme
{
public interface IBasePanel : IController
{
2025-08-29 11:01:03 +08:00
/// <summary>
/// UI信息
/// </summary>
string UIName { get; set; }
2025-10-14 14:29:08 +08:00
2025-04-10 18:15:01 +08:00
/// <summary>
/// 绑定这个面板的实例
/// </summary>
void Initialize(GameObject panelGo);
2025-12-12 15:58:37 +08:00
2025-10-31 11:18:23 +08:00
/// <summary>
/// 绑定这个面板的实例
/// </summary>
Task InitializeAsync(GameObject panelGo);
2025-12-12 15:58:37 +08:00
2025-04-10 18:15:01 +08:00
/// <summary>
/// 初始化面板管理器
/// </summary>
/// <param name="panelManager"></param>
void Initialize(IPanelSystem sysytem);
2025-12-12 15:58:37 +08:00
2025-10-31 11:18:23 +08:00
/// <summary>
/// 初始化面板管理器
/// </summary>
/// <param name="panelManager"></param>
Task InitializeAsync(IPanelSystem sysytem);
2025-12-12 15:58:37 +08:00
2025-04-10 18:15:01 +08:00
/// <summary>
/// 虚方法UI进入时执行的操作只会执行一次
/// </summary>
2025-10-14 14:29:08 +08:00
void OnEnter(Action complete = null);
2025-04-10 18:15:01 +08:00
/// <summary>
/// 虚方法UI退出时执行的操作只会执行一次
/// </summary>
void OnExit(float delay = 0f);
void OnDestroy();
/// <summary>
/// 生成/获取一个UI对象
/// </summary>
/// <param name="type">ui信息</param>
/// <returns></returns>
2025-12-12 15:58:37 +08:00
Task<GameObject> CreatePanel(string packageName, string panelName);
2025-04-10 18:15:01 +08:00
/// <summary>
/// 销毁一个Ui对象
/// </summary>
/// <param name="type"></param>
void DestoryUI();
2025-04-27 11:30:35 +08:00
2025-04-10 18:15:01 +08:00
void SetPanelParent(Transform parent);
}
/// <summary>
///所有UI面板的父类包含UI面板的状态信息
/// </summary>
public abstract class BasePanel : IBasePanel
{
/// <summary>
/// UI信息
/// </summary>
2025-08-29 11:01:03 +08:00
public string UIName { get; set; }
2025-04-10 18:15:01 +08:00
2025-10-14 14:29:08 +08:00
public abstract UITweenType TweenType { get; }
2025-12-12 15:58:37 +08:00
public abstract UIInstantiateType UIType { get; }
2025-04-10 18:15:01 +08:00
/// <summary>
/// 面板管理器
/// </summary>
protected IPanelSystem PanelSystem { get; private set; }
/// <summary>
/// 生成的父物体
/// </summary>
2025-04-27 11:30:35 +08:00
protected Transform panelParent { get; private set; }
2025-04-10 18:15:01 +08:00
2025-12-12 15:58:37 +08:00
/// <summary>
/// 缓存该面板所有的SpriteRenderer
/// </summary>
private Dictionary<string, SpriteRenderer> spriteRendererDict;
2025-04-10 18:15:01 +08:00
/// <summary>
/// 缓存该面板所有的ButtonToggleSliderInputFieldDropdownScroolBar
/// </summary>
private Dictionary<string, Selectable> selectableDict;
2025-04-27 11:30:35 +08:00
protected GameObject activePanel { get; private set; }
2025-04-10 18:15:01 +08:00
2025-04-21 18:23:53 +08:00
protected BasePanel()
2025-04-10 18:15:01 +08:00
{
2025-12-12 15:58:37 +08:00
spriteRendererDict = new Dictionary<string, SpriteRenderer>();
2025-04-10 18:15:01 +08:00
selectableDict = new Dictionary<string, Selectable>();
}
public virtual void Initialize(GameObject panelGo)
{
activePanel = panelGo;
2025-12-12 15:58:37 +08:00
if (UIType == UIInstantiateType.SpriteNormal)
2025-04-10 18:15:01 +08:00
{
2025-12-12 15:58:37 +08:00
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;
2025-04-10 18:15:01 +08:00
}
2025-12-12 15:58:37 +08:00
2025-04-10 18:15:01 +08:00
}
2025-10-31 11:18:23 +08:00
public virtual Task InitializeAsync(GameObject panelGo)
{
Initialize(panelGo);
return Task.CompletedTask;
}
2025-04-10 18:15:01 +08:00
public virtual void Initialize(IPanelSystem sysytem)
{
PanelSystem = sysytem;
}
2025-10-31 11:18:23 +08:00
public virtual Task InitializeAsync(IPanelSystem sysytem)
{
Initialize(sysytem);
return Task.CompletedTask;
}
2025-04-10 18:15:01 +08:00
2025-10-14 14:29:08 +08:00
public virtual void OnEnter(Action complete = null)
2025-04-10 18:15:01 +08:00
{
activePanel.SetActive(true);
2025-10-14 14:29:08 +08:00
2025-04-10 18:15:01 +08:00
2025-12-12 15:58:37 +08:00
SetCanvasGroupTween(1f, complete);
2025-04-10 18:15:01 +08:00
}
public virtual void OnExit(float delay = 0f)
{
2025-12-12 15:58:37 +08:00
SetCanvasGroupTween(0f, () => { activePanel.SetActive(false); });
2025-04-10 18:15:01 +08:00
}
public virtual void OnDestroy()
{
DestoryUI();
}
2025-12-12 15:58:37 +08:00
public async Task<GameObject> CreatePanel(string packageName, string panelName)
2025-04-10 18:15:01 +08:00
{
if (panelParent == null)
{
2025-08-08 12:59:27 +08:00
Debug.LogWarning($"UnityEvo:parent为空,{panelName}自动设置为root节点下请检查是否正确");
2025-04-10 18:15:01 +08:00
}
2025-04-27 11:30:35 +08:00
2025-04-10 18:15:01 +08:00
if (this.activePanel != null)
{
return activePanel.gameObject;
}
2025-06-25 15:51:50 +08:00
#if HotUpdate
2025-04-27 11:30:35 +08:00
2025-04-10 18:15:01 +08:00
await handle.Task;
2025-12-12 15:58:37 +08:00
var prefab = await AMP.ResourceLoader.LoadAssetAsync<GameObject>(packageName, panelName);
2025-04-27 11:30:35 +08:00
activePanel = GameObject.Instantiate(handle.AssetObject as GameObject, panelParent);
2025-06-25 15:51:50 +08:00
#else
GameObject handle = Resources.Load<GameObject>(panelName);
activePanel = GameObject.Instantiate(handle, panelParent);
#endif
2025-12-12 15:58:37 +08:00
2025-04-10 18:15:01 +08:00
activePanel.name = this.GetType().Name;
2025-04-27 11:30:35 +08:00
2025-12-12 15:58:37 +08:00
// if (!activePanel.GetComponentInChildren<Canvas>() && !activePanel.GetComponentInParent<Canvas>())
// {
// Debug.LogError($"UnityEvo:panelParent上不存在Canvas组件,{panelName}无法正常运行,进程已中断,请检查!!!!!");
// return null;
// }
2025-04-27 11:30:35 +08:00
2025-04-10 18:15:01 +08:00
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;
}
2025-12-12 15:58:37 +08:00
private void SetCanvasGroup(float alpha)
2025-04-10 18:15:01 +08:00
{
2025-12-12 15:58:37 +08:00
foreach (var spriteRenderer in spriteRendererDict)
2025-04-10 18:15:01 +08:00
{
2025-12-12 15:58:37 +08:00
Color c = spriteRenderer.Value.color;
c.a = alpha;
spriteRenderer.Value.color = c;
2025-04-10 18:15:01 +08:00
}
2025-12-12 15:58:37 +08:00
}
private void SetCanvasGroupTween(float alpha, Action complete = null)
{
if (UIType == UIInstantiateType.SpriteNormal)
{
// 创建一个新的序列
Sequence sequence = DOTween.Sequence();
2025-04-27 11:30:35 +08:00
2025-12-12 15:58:37 +08:00
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(); });
}
}
2025-04-10 18:15:01 +08:00
}
public IArchitecture GetArchitecture()
{
return PanelSystem.GetArchitecture();
}
2025-10-14 14:29:08 +08:00
public enum UITweenType
{
/// <summary>
/// 无动画
/// </summary>
None,
/// <summary>
/// 淡入淡出
/// </summary>
Fade,
/// <summary>
/// 循环播放
/// </summary>
Loop,
/// <summary>
/// 往返播放
/// </summary>
Yoyo,
}
2025-12-12 15:58:37 +08:00
public enum UIInstantiateType
{
/// <summary>
/// 普通面板
/// </summary>
SpriteNormal,
/// <summary>
/// UI普通面板
/// </summary>
UINormal
}
2025-04-10 18:15:01 +08:00
}
2025-04-27 11:30:35 +08:00
}