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

303 lines
8.4 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-10-31 11:18:23 +08:00
/// <summary>
/// 绑定这个面板的实例
/// </summary>
Task InitializeAsync(GameObject panelGo);
2025-04-10 18:15:01 +08:00
/// <summary>
/// 初始化面板管理器
/// </summary>
/// <param name="panelManager"></param>
void Initialize(IPanelSystem sysytem);
2025-10-31 11:18:23 +08:00
/// <summary>
/// 初始化面板管理器
/// </summary>
/// <param name="panelManager"></param>
Task InitializeAsync(IPanelSystem sysytem);
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 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>
2025-04-27 11:30:35 +08:00
Task<GameObject> CreatePanel(string panelName, string packageName);
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-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
/// <summary>
/// 缓存该面板所有的ButtonToggleSliderInputFieldDropdownScroolBar
/// </summary>
private Dictionary<string, Selectable> selectableDict;
2025-04-27 11:30:35 +08:00
protected CanvasGroup canvasGroup { get; private set; }
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
{
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;
}
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);
canvasGroup.blocksRaycasts = true;
2025-10-31 11:18:23 +08:00
if (TweenType == UITweenType.None)
2025-10-14 14:29:08 +08:00
{
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(); });
}
2025-04-10 18:15:01 +08:00
}
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();
}
2025-04-27 11:30:35 +08:00
public async Task<GameObject> CreatePanel(string panelName, string packageName)
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
AssetHandle handle = null;
if (packageName == null)
{
handle = YooAssets.LoadAssetAsync<GameObject>(panelName);
}
else
{
2025-10-14 14:29:08 +08:00
var package = YooAssets.TryGetPackage(packageName);
if (package == null)
{
handle = YooAssets.LoadAssetAsync<GameObject>(panelName);
}
else
{
handle = package.LoadAssetAsync<GameObject>(panelName);
}
2025-04-27 11:30:35 +08:00
}
2025-04-10 18:15:01 +08:00
await handle.Task;
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-04-10 18:15:01 +08:00
activePanel.name = this.GetType().Name;
2025-04-27 11:30:35 +08:00
2025-10-14 14:29:08 +08:00
if (!activePanel.GetComponentInChildren<Canvas>() && !activePanel.GetComponentInParent<Canvas>())
2025-04-22 10:03:07 +08:00
{
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;
}
/// <summary>
/// 获取组件ButtonToggleSliderInputFieldDropdownScroolBar
/// </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不存在");
}
2025-04-27 11:30:35 +08:00
2025-04-10 18:15:01 +08:00
return t as T;
}
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-04-10 18:15:01 +08:00
}
2025-04-27 11:30:35 +08:00
}