NLDClient-yudde/ProjectNLD/Assets/Code/Scripts/Framework/UI/UIWindow.Lifecycle.cs

477 lines
17 KiB
C#
Raw Normal View History

2025-09-22 17:45:42 +08:00
using Cysharp.Threading.Tasks;
using DG.Tweening;
using Framework;
using PhxhSDK;
using UnityEngine.UI;
public abstract partial class UIWindow
{
2025-09-10 16:05:21 +08:00
/*
2025-12-29 14:43:15 +08:00
UIWindow (Lifecycle)
1. Load UIManager.CreateWindow()
2. Init Init(data) UI
3. Show ShowWindow(data)
4. Update Update(dt)
5. Refresh Refresh(data)
6. Close CloseWindow(false)
7. Destroy CloseWindow(true)
OnInit() - UI
OnBeforeShow() - false
OnAfterShow() -
OnUpdate(dt) -
OnRefresh() -
OnHide() -
OnRelease() -
Init, ShowWindow, Update
OnXxx
CloseWindow(true)CloseWindow(false)
2025-09-10 16:05:21 +08:00
*/
2025-09-12 15:18:50 +08:00
#region Lifecycle Management
2025-09-10 21:02:34 +08:00
/// <summary>
/// 初始化窗口
/// </summary>
2025-09-10 16:05:21 +08:00
public void Init(object data = null)
{
try
{
OnBeforeInit(data);
if (isInited)
{
DebugUtil.LogWarning("window : {0} already inited", WindowName);
}
else
{
DoInit();
OnInit();
isInited = true;
}
2025-09-10 16:05:21 +08:00
}
catch (System.Exception e)
2025-09-10 16:05:21 +08:00
{
2025-11-04 16:08:13 +08:00
DebugUtil.LogError("window : {0} init window error: {1}\n===Stack===\n{2}\n======", WindowName, e.Message, e.StackTrace);
}
2025-09-10 16:05:21 +08:00
}
2025-09-12 15:18:50 +08:00
/// <summary>
/// 显示窗口
/// </summary>
public void ShowWindow(object data = null)
{
try
{
if (_isShow)
{
DebugUtil.LogWarning("window : {0} is show, can't show again", WindowName);
}
2025-12-25 15:01:35 +08:00
// 🎯 在显示前截图(如果需要)
CaptureScreenshotIfNeeded();
// 🎯 自动设置模糊背景(如果需要)
SetupBlurBackgroundIfNeeded();
2025-09-12 15:18:50 +08:00
if (!OnBeforeShow(data))
{
DebugUtil.LogError("window : {0} on before show return false", WindowName);
return;
}
DoShow();
OnShow(data);
if (gameObject is null)
2025-09-12 15:18:50 +08:00
{
DebugUtil.Log("UI已被销毁:{0}", WindowName);
}
else
{
gameObject.SetActive(true);
}
_isShow = true;
2025-12-25 20:33:19 +08:00
OnAfterShow(data);
2025-09-22 17:45:42 +08:00
bool isShowOpenAnim = IsShowOpenAnim;
if (isShowOpenAnim)
{
2025-12-25 20:33:19 +08:00
2025-09-22 17:45:42 +08:00
AnimShow(() =>
{
2025-12-25 20:33:19 +08:00
//TODO
2025-09-22 17:45:42 +08:00
EventManager.Instance.Send(EventManager.EventName.UIOpen, WindowName);
}).Forget();
}
else
{
2025-12-25 20:33:19 +08:00
//OnAfterShow(data);
2025-09-22 17:45:42 +08:00
EventManager.Instance.Send(EventManager.EventName.UIOpen, WindowName);
}
2025-09-12 15:18:50 +08:00
}
catch (System.Exception e)
{
2025-11-04 16:08:13 +08:00
DebugUtil.LogError("window : {0} show window error: {1}\n===Stack===\n{2}\n======", WindowName, e.Message, e.StackTrace);
2025-09-12 15:18:50 +08:00
}
}
2025-12-25 15:01:35 +08:00
/// <summary>
2025-12-29 14:43:15 +08:00
/// 在显示前截图(如果需要)
2025-12-25 15:01:35 +08:00
/// </summary>
private void CaptureScreenshotIfNeeded()
{
var config = UIManager.Instance.GetWindowInfo(WindowName);
if (config.NeedScreenshot)
{
2025-12-29 18:41:38 +08:00
var oldActive = gameObject.activeSelf;
gameObject.SetActive(false);
2025-12-25 15:01:35 +08:00
ScreenshotManager.Instance.CaptureScreenshot();
2025-12-29 18:41:38 +08:00
gameObject.SetActive(oldActive);
2025-12-25 15:01:35 +08:00
}
}
/// <summary>
2025-12-29 14:43:15 +08:00
/// 自动设置模糊背景(如果需要)
2025-12-25 15:01:35 +08:00
/// </summary>
private void SetupBlurBackgroundIfNeeded()
{
var config = UIManager.Instance.GetWindowInfo(WindowName);
if (!config.NeedScreenshot) return;
// 查找约定的 GaussianBlurMask 节点
var blurTransform = transform.Find("GaussianBlurMask");
if (blurTransform != null)
{
var blurMask = blurTransform.GetComponent<UnityEngine.UI.RawImage>();
if (blurMask != null)
{
blurMask.texture = ScreenshotManager.Instance.GetScreenTexture2D();
blurMask.gameObject.SetActive(true);
}
}
}
2025-09-10 21:02:34 +08:00
/// <summary>
2025-12-29 18:41:38 +08:00
/// 更新入口,界面打开的时候更新
2025-09-10 21:02:34 +08:00
/// </summary>
2025-09-12 15:18:50 +08:00
public void Update(float dt)
2025-09-10 16:05:21 +08:00
{
try
{
_notchScreenAdapter?.UpdateCheck();
OnUpdate(dt);
}
catch (System.Exception e)
{
2025-11-04 16:08:13 +08:00
DebugUtil.LogError("window : {0} update window error: {1}\n===Stack===\n{2}\n======", WindowName, e.Message, e.StackTrace);
}
}
2025-09-12 15:18:50 +08:00
2025-09-10 21:02:34 +08:00
/// <summary>
2025-09-12 15:18:50 +08:00
/// 刷新窗口数据
2025-09-10 21:02:34 +08:00
/// </summary>
public void Refresh(object data)
2025-09-10 21:02:34 +08:00
{
try
2025-09-10 21:02:34 +08:00
{
if (isInited)
{
OnRefresh(data);
}
else
{
DebugUtil.LogError("window : {0} not inited", WindowName);
}
2025-09-10 21:02:34 +08:00
}
catch (System.Exception e)
2025-09-10 21:02:34 +08:00
{
2025-11-04 16:08:13 +08:00
DebugUtil.LogError("window : {0} refresh with data error: {1}\n===Stack===\n{2}\n======", WindowName, e.Message, e.StackTrace);
2025-09-10 21:02:34 +08:00
}
}
2025-09-12 15:18:50 +08:00
/// <summary>
/// 关闭窗口
2025-12-29 14:43:15 +08:00
/// destroy = false: 关闭但保留内存(快速重开)
/// destroy = true: 销毁并释放资源
2025-09-12 15:18:50 +08:00
/// </summary>
public async void CloseWindow(bool destroy)
2025-09-12 15:18:50 +08:00
{
try
{
// 如果窗口已关闭
if (!_isShow)
2025-09-12 15:18:50 +08:00
{
// 如果不需要销毁,直接返回
if (!destroy)
2025-09-12 15:18:50 +08:00
{
DebugUtil.LogWarning("window : {0} is already closed", WindowName);
return;
2025-09-12 15:18:50 +08:00
}
// 如果需要销毁,跳过关闭逻辑,直接执行销毁
DebugUtil.LogG($"窗口已关闭直接销毁UI:{WindowName}");
IsClosing = true;
UIManager.Instance.RemoveWindow(mWindowName);
DoDestroy();
IsClosing = false;
return;
2025-09-12 15:18:50 +08:00
}
IsClosing = true;
// 1. 恢复主相机
RestoreMainCamera();
// 2. 获取窗口配置
var config = UIManager.Instance.GetWindowInfo(WindowName);
2025-12-25 20:26:05 +08:00
// 3. 可见性管理(由 UIWindowVisibilityManager 根据配置决定是否处理)
UIWindowVisibilityManager.Instance.HandleVisibilityOnClose(this, config.VisibilityStrategy);
2025-12-25 20:26:05 +08:00
// 4. 隐藏逻辑
2025-09-12 15:18:50 +08:00
OnHide();
gameObject?.SetActive(false);
_isShow = false;
EventManager.Instance.Send(EventManager.EventName.UIHide, WindowName);
2025-12-25 20:26:05 +08:00
// 5. 播放关闭动画
bool isShowCloseAnim = IsShowOpenAnim;
if (isShowCloseAnim)
{
UIManager.Instance.BlockUIForCloseOperation();
await AnimHide(() =>
{
// 动画播放完毕后的回调
if (destroy)
{
DoDestroy();
UIManager.Instance.RemoveWindow(mWindowName);
}
IsClosing = false;
});
}
else
{
// 7. 如果需要销毁(无动画情况)
if (destroy)
{
DoDestroy();
UIManager.Instance.RemoveWindow(mWindowName);
}
IsClosing = false;
}
}
catch (System.Exception e)
{
2025-11-04 16:08:13 +08:00
DebugUtil.LogError("window : {0} close window error: {1}\n===Stack===\n{2}\n======", WindowName, e.Message, e.StackTrace);
}
}
2025-09-10 21:02:34 +08:00
#endregion
2025-12-26 12:16:37 +08:00
#region User Interaction
#endregion
2025-12-29 14:43:15 +08:00
2025-09-12 15:18:50 +08:00
#region Internal Implementation
/// <summary>
/// 恢复主相机状态
/// </summary>
private void RestoreMainCamera()
{
var config = UIManager.Instance.GetWindowInfo(WindowName);
2025-12-24 14:40:00 +08:00
if (config.VisibilityStrategy == UIVisibilityStrategy.Occlusion && _shutMainCamera)
{
if (!CameraManager.Instance.CheckMainCameraIsActive())
{
CameraManager.Instance.SetMainCameraActive(true);
}
}
}
2025-09-12 15:18:50 +08:00
/// <summary>
/// 执行基础初始化
/// </summary>
private void DoInit()
{
_isShow = false;
2025-12-26 17:02:41 +08:00
var metaInfo = UIManager.Instance.GetWindowInfo(WindowName);
_shutMainCamera = metaInfo.ShutMainCamera;
IsShowOpenAnim = metaInfo.IsAutoAnim;
BackStrategy = UIBackStrategyFactory.CreateStrategy(metaInfo.BackStrategyType);
2025-09-12 15:18:50 +08:00
AutoBindField();
InitScreenAdaption();
BindDefaultCloseButtons();
BindDefaultHomeButtons();
BindDefaultBackButtons();
2025-09-12 15:18:50 +08:00
}
2025-09-10 21:02:34 +08:00
/// <summary>
2025-09-12 15:18:50 +08:00
/// 执行显示逻辑
/// </summary>
2025-09-10 19:54:45 +08:00
private void DoShow()
{
if (UIManager.Instance.WindowInfos.TryGetValue(WindowName, out var windowInfo))
{
if (CameraManager.Instance.CheckMainCameraIsActive())
{
2025-12-24 14:40:00 +08:00
if (windowInfo.VisibilityStrategy == UIVisibilityStrategy.Occlusion)
{
2025-09-12 15:18:50 +08:00
// 特殊处理主界面
if (_shutMainCamera)
{
CameraManager.Instance.SetMainCameraActive(false);
}
}
}
}
}
2025-12-29 14:43:15 +08:00
/// <summary>
2025-12-29 14:43:15 +08:00
/// 执行销毁逻辑
/// </summary>
private void DoDestroy()
{
DebugUtil.LogG($"DoDestroy:{WindowName}");
// 如果窗口还在显示状态,先调用 OnHide
if (_isShow)
{
OnHide();
_isShow = false;
}
// 确保 GameObject 隐藏
if (gameObject != null)
{
gameObject.SetActive(false);
}
// 发送隐藏事件
EventManager.Instance.Send(EventManager.EventName.UIHide, WindowName);
// 资源释放
if (gameObject)
{
2025-09-09 20:26:41 +08:00
if (isInited)
{
OnRelease();
2025-09-12 15:18:50 +08:00
DoRelease();
2025-09-09 20:26:41 +08:00
isInited = false;
}
2025-09-12 15:18:50 +08:00
if (UIManager.Instance.UIWindowObjDic.ContainsKey(gameObject))
{
UIManager.Instance.UIWindowObjDic.Remove(gameObject);
}
UnityEngine.Object.Destroy(gameObject);
}
// 卸载资源
AssetManager.Instance.Unload(string.Format(UIManager.UI_PREFAB_PATH, mWindowName), true);
gameObject = null;
}
2025-09-10 19:54:45 +08:00
/// <summary>
2025-09-12 15:18:50 +08:00
/// 基础资源释放
/// </summary>
2025-09-12 15:18:50 +08:00
private void DoRelease()
{
2025-09-12 15:18:50 +08:00
EventManager.Instance.Send(EventManager.EventName.UIDestroy, WindowName);
2025-09-12 10:54:10 +08:00
2025-09-12 15:18:50 +08:00
UnLoad();
_openAnimSeq?.Kill();
_assetReferenceManager?.Dispose();
_assetReferenceManager = null;
_notchScreenAdapter = null;
_graphicRaycasterManager = null;
}
#endregion
2025-09-12 15:18:50 +08:00
#region Virtual Methods
2025-09-12 15:18:50 +08:00
// Init Phase
/// <summary>
2025-12-29 14:43:15 +08:00
/// 初始化前预处理已废弃建议使用OnInit
/// </summary>
2025-09-24 20:46:04 +08:00
[System.Obsolete("OnBeforeInit已废弃请使用OnInit方法替代。OnInit提供更清晰的初始化流程和更好的代码组织。")]
protected virtual void OnBeforeInit(object data = null){}
/// <summary>
2025-09-12 15:18:50 +08:00
/// UI初始化
/// </summary>
protected virtual void OnInit(){}
2025-09-12 15:18:50 +08:00
/// <summary>
/// 自动绑定字段
/// </summary>
protected virtual void AutoBindField(){}
2025-09-11 16:51:41 +08:00
2025-09-12 15:18:50 +08:00
// Show Phase
/// <summary>
2025-12-29 14:43:15 +08:00
/// 显示前检查返回false可取消显示
2025-09-12 15:18:50 +08:00
/// </summary>
2025-09-12 10:54:10 +08:00
protected virtual bool OnBeforeShow(object data)
{
return true;
}
2025-09-11 16:39:35 +08:00
/// <summary>
2025-12-29 14:43:15 +08:00
/// 显示时处理已废弃建议使用OnAfterShow
2025-09-11 16:39:35 +08:00
/// </summary>
2025-09-24 20:46:04 +08:00
[System.Obsolete("OnShow已废弃请使用OnAfterShow方法替代。OnAfterShow在窗口完全显示后调用提供更稳定的显示时机。")]
protected virtual void OnShow(object data){}
/// <summary>
2025-09-12 15:18:50 +08:00
/// 显示后处理
/// </summary>
protected virtual void OnAfterShow(object data){}
2025-09-12 15:18:50 +08:00
/// <summary>
2025-09-12 15:18:50 +08:00
/// 每帧更新
/// </summary>
protected virtual void OnUpdate(float dt){}
2025-09-12 15:18:50 +08:00
// Refresh Phase
/// <summary>
2025-09-12 15:18:50 +08:00
/// 数据刷新
/// </summary>
protected virtual void OnRefresh(object data) {}
2025-09-12 15:18:50 +08:00
// Hide Phase
/// <summary>
2025-09-12 15:18:50 +08:00
/// 隐藏时处理
/// </summary>
protected virtual void OnHide(){}
2025-09-12 15:18:50 +08:00
// Release Phase
/// <summary>
2025-09-12 15:18:50 +08:00
/// 资源释放
/// </summary>
protected virtual void OnRelease(){}
2025-09-12 15:18:50 +08:00
#endregion
}