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

562 lines
33 KiB
C#
Raw Normal View History

using System;
using Cysharp.Threading.Tasks;
using DG.Tweening;
using Framework;
using PhxhSDK;
using UnityEngine;
using UnityEngine.UI;
/// <summary>
/// UI窗口基类 - 生命周期管理部分
2025-09-12 15:18:50 +08:00
///
/// 功能概述:
/// - 窗口生命周期管理Init → Show → Update → Hide → Close
/// - 资源加载与释放管理
/// - 屏幕适配与相机控制
/// - 动画播放与UI事件处理
///
/// 设计特点:
/// - Init和Show分离设计支持窗口预初始化
/// - 完整的生命周期钩子函数,便于子类扩展
/// - 异常安全的显示/隐藏逻辑
/// - 自动资源管理和内存清理
/// </summary>
public abstract partial class UIWindow
{
2025-09-10 16:05:21 +08:00
/*
UIWindow (Lifecycle)
1. Load -
UIRoot.CreateWindow()
GameObject
UIWindow
2. Init -
UIWindow.Init(data) // 窗口初始化(独立调用) ║
*OnBeforeInit(data) // 预处理数据 (原 PreLoad
DoInit() // 基础初始化 ║
AutoBindField() // 绑定UI组件引用 ║
InitScreenAdaption() // 屏幕适配 ║
_TryBindBgClose() // 绑定通用事件 ║
*OnInit() // 初始化UI状态设置默认值 ║
3. Show -
UIWindow.ShowWindow(data) // 显示窗口(公共接口) ║
2025-09-11 16:51:41 +08:00
DoShow() // 处理相机状态等显示逻辑 ║
*OnShow(data) // 显示时回调,业务逻辑处理 ║
SetActive(true) // 激活GameObject ║
*OnAfterShow(data) // 显示后回调,最终处理 ║
UI // EventManager.Send ║
2025-09-10 16:05:21 +08:00
4. Update -
2025-09-10 21:02:34 +08:00
Update(dt) -> *OnUpdate(dt) // 每帧更新处理UI逻辑合并原 Update
2025-09-10 16:05:21 +08:00
5. Refresh -
2025-09-10 21:02:34 +08:00
RefreshWithData(data) // 数据刷新(原 ReloadWindow
*OnDataRefresh(data) // 刷新UI内容更新显示数据原 OnReloadWindow
2025-09-10 16:05:21 +08:00
6. Hide -
HideWindow() -> Hide()
SetActive(false) // 隐藏GameObject ║
2025-09-12 15:18:50 +08:00
*OnHide() // 隐藏回调 ║
2025-09-10 16:05:21 +08:00
7. Close -
CloseWindow() -> DestroyWindow()
2025-09-12 15:18:50 +08:00
*OnHide() // 隐藏回调 ║
2025-09-10 16:05:21 +08:00
*OnRelease() // 释放资源,清理引用 ║
Destroy(gameObject) // 销毁GameObject ║
: InitShow
使: Init(data)ShowWindow(data)
:
:
- *
🔸 *OnBeforeInit(data) -
DoInitAutoBindFieldInitScreenAdaption OnInit
UI
OpenData
2025-09-12 15:18:50 +08:00
base.OnBeforeInit()
2025-09-10 16:05:21 +08:00
🔸 *OnInit() - UI
DoInit
UI
base.OnInit()
2025-09-11 16:51:41 +08:00
🔸 *OnShow(data) -
SetActive
UI
UI
2025-09-12 15:18:50 +08:00
base.OnShow(data)
2025-09-11 16:51:41 +08:00
🔸 *OnAfterShow(data) -
SetActive
2025-09-12 15:18:50 +08:00
base.OnAfterShow(data)
2025-09-10 16:05:21 +08:00
🔸 *OnUpdate(dt) -
2025-09-12 15:18:50 +08:00
base.OnUpdate(dt)
2025-09-10 16:05:21 +08:00
🔸 *OnDataRefresh(data) -
RefreshWithData
base.OnDataRefresh(data)
2025-09-12 15:18:50 +08:00
🔸 *OnHide() -
2025-09-10 16:05:21 +08:00
2025-09-12 15:18:50 +08:00
base.OnHide()
2025-09-10 16:05:21 +08:00
🔸 *OnRelease() -
Tween
2025-09-12 15:18:50 +08:00
base.OnRelease()
2025-09-10 16:05:21 +08:00
2025-09-12 15:18:50 +08:00
AutoBindField:
2025-09-10 16:05:21 +08:00
RefreshWithData: OnDataRefresh
Init: OnBeforeInitDoInitOnInit
ShowWindow: Show
: InitShow
2025-09-12 15:18:50 +08:00
base.OnXxx()
2025-09-10 16:05:21 +08:00
*/
2025-09-10 21:02:34 +08:00
#region Private Helper Methods
/// <summary>
2025-09-12 15:18:50 +08:00
/// 绑定背景关闭按钮
/// </summary>
private void _TryBindBgClose()
{
var trans = transform.Find("BgClose");
if (trans)
{
var btn = trans.GetComponent<Button>();
if (btn)
{
BindButton(btn, () =>
{
var clickAudio = btn.GetComponent<ClickAudio>();
if (clickAudio)
{
if (clickAudio.IsPlayAudio && !string.IsNullOrEmpty(clickAudio.AudioKey))
2025-09-12 15:18:50 +08:00
{
AudioManager.Instance.PlayAudio(clickAudio.AudioKey);
2025-09-12 15:18:50 +08:00
}
else
{
CommonUtilsFramework.PlayUICommonSound();
}
}
if (_onBgCloseFunc == null)
{
CloseWindow(UIWindowCloseType.HideAndDestroy);
return;
}
_onBgCloseFunc?.Invoke();
});
}
}
}
2025-09-10 21:02:34 +08:00
#endregion
2025-09-12 15:18:50 +08:00
#region Lifecycle Management
2025-09-10 21:02:34 +08:00
/// <summary>
/// 初始化窗口
/// </summary>
2025-09-12 15:18:50 +08:00
/// <param name="data">初始化数据</param>
2025-09-10 16:05:21 +08:00
public void Init(object data = null)
{
2025-09-10 16:05:21 +08:00
OnBeforeInit(data);
if (isInited)
{
2025-09-10 16:05:21 +08:00
DebugUtil.LogWarning("window : {0} already inited", WindowName);
}
else
{
DoInit();
OnInit();
2025-09-09 20:26:41 +08:00
isInited = true;
}
2025-09-10 16:05:21 +08:00
}
2025-09-12 15:18:50 +08:00
/// <summary>
/// 显示窗口
/// </summary>
/// <param name="data">显示数据</param>
public void ShowWindow(object data = null)
{
// 未被唤醒不执行Show
if (IsUnEnabled)
{
return;
}
try
{
if (_isShow)
{
DebugUtil.LogWarning("window : {0} is show, can't show again", WindowName);
}
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 == null)
{
DebugUtil.Log("UI已被销毁:{0}", WindowName);
}
else
{
gameObject.SetActive(true);
}
_isShow = true;
OnAfterShow(data);
EventManager.Instance.Send(EventManager.EventName.UIOpen, WindowName);
}
catch (System.Exception e)
{
DebugUtil.LogError("window : {0} show window error: {1}", WindowName, e.Message);
}
}
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>
2025-09-12 15:18:50 +08:00
public void Update(float dt)
2025-09-10 16:05:21 +08:00
{
2025-09-12 15:18:50 +08:00
_notchScreenAdapter?.UpdateCheck();
OnUpdate(dt);
}
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>
2025-09-12 15:18:50 +08:00
/// <param name="data">新数据</param>
2025-09-10 21:02:34 +08:00
public void RefreshWithData(object data)
{
if (isInited)
{
OnDataRefresh(data);
}
else
{
DebugUtil.LogError("window : {0} not inited", WindowName);
}
}
2025-09-12 15:18:50 +08:00
/// <summary>
/// 隐藏窗口
/// </summary>
public void HideWindow()
{
if (!_isShow)
{
DebugUtil.LogWarning("window : {0} is hide, can't hide again", WindowName);
}
try
{
if (UIManager.Instance.WindowInfos.TryGetValue(WindowName, out var windowInfo))
{
if (windowInfo.UIOpenType == UIOpenType.FullScreen)
{
if (!CameraManager.Instance.CheckMainCameraIsActive())
{
CameraManager.Instance.SetMainCameraActive(true);
}
}
}
OnHide();
gameObject?.SetActive(false);
_isShow = false;
EventManager.Instance.Send(EventManager.EventName.UIHide, WindowName);
}
catch (System.Exception e)
{
DebugUtil.LogError("window : {0} hide window error: {1}", WindowName, e.Message);
}
}
/// <summary>
/// 关闭窗口
/// </summary>
/// <param name="closeType">关闭类型</param>
public async void CloseWindow(UIWindowCloseType closeType)
{
IsClosing = true;
DebugUtil.LogG($"关闭UI:{WindowName}, closeType{closeType}");
if (UIManager.Instance.CheckWindowIsFullScreen(this))
{
if (_shutMainCamera)
{
CameraManager.Instance.SetMainCameraActive(true);
}
}
if (closeType == UIWindowCloseType.HideAndDestroy)
{
var last = UIManager.Instance.GetLast();
if (last == this)
{
DebugUtil.LogG($"PopWindow{this.WindowName}");
UIManager.Instance.PopWindow();
}
2025-09-12 15:18:50 +08:00
2025-09-12 15:54:45 +08:00
if (UIManager.Instance.UiWindowStack.Count > 0)
{
if (UIManager.Instance.CheckWindowIsFullScreen(this))
2025-09-12 15:18:50 +08:00
{
2025-09-12 15:54:45 +08:00
var window = UIManager.Instance.GetLastFullScreen();
if (window != null && window != this)
{
if (UIManager.Instance.CheckIsWindowInStack(this))
{
UIManager.Instance.ForceRemoveWindow(this);
DebugUtil.LogG($"UI出栈,{WindowName}");
}
}
for (int i = UIManager.Instance.UiWindowStack.Count - 1; i >= 0; i--)
2025-09-12 15:18:50 +08:00
{
2025-09-12 15:54:45 +08:00
var latestWindow = UIManager.Instance.UiWindowStack[i];
latestWindow.ShowWindow();
if (UIManager.Instance.CheckWindowIsFullScreen(latestWindow))
break;
2025-09-12 15:18:50 +08:00
}
}
2025-09-12 15:54:45 +08:00
else
2025-09-12 15:18:50 +08:00
{
2025-09-12 15:54:45 +08:00
UIManager.Instance.ForceRemoveWindow(this);
2025-09-12 15:18:50 +08:00
}
}
2025-09-12 15:54:45 +08:00
2025-09-12 15:18:50 +08:00
2025-09-12 15:54:45 +08:00
bool isShowCloseAnim = IsShowOpenAnim;
if (isShowCloseAnim)
{
2025-09-12 15:18:50 +08:00
UIManager.Instance.BlockUIForCloseOperation();
await AnimHide(() =>
{
DestroyWindow();
UIManager.Instance.RemoveWindow(mWindowName);
IsClosing = false;
});
}
else
{
UIManager.Instance.RemoveWindow(mWindowName);
DestroyWindow();
IsClosing = false;
}
}
2025-09-12 15:18:50 +08:00
else if (closeType == UIWindowCloseType.ForceDestroy)
{
2025-09-12 15:18:50 +08:00
// 强制清除所有界面时使用
DestroyWindow();
UIManager.Instance.RemoveWindow(mWindowName);
IsClosing = false;
}
}
2025-09-10 21:02:34 +08:00
#endregion
2025-09-12 15:18:50 +08:00
#region Internal Implementation
/// <summary>
/// 执行基础初始化
/// </summary>
private void DoInit()
{
_isShow = false;
_shutMainCamera = UIManager.Instance.IsShutMainCamera(WindowName);
IsShowOpenAnim = UIManager.Instance.GetIsAutoPlayAnim(WindowName);
AutoBindField();
InitScreenAdaption();
_TryBindBgClose();
}
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())
{
if (windowInfo.UIOpenType == UIOpenType.FullScreen)
{
2025-09-12 15:18:50 +08:00
// 特殊处理主界面
if (_shutMainCamera && WindowName != UINameConst.UI_MainPanel)
{
CameraManager.Instance.SetMainCameraActive(false);
}
}
}
}
}
/// <summary>
/// 销毁窗口
/// </summary>
private void DestroyWindow()
{
DebugUtil.LogG($"DestroyWindow:{WindowName}");
2025-09-12 15:18:50 +08:00
OnHide();
gameObject?.SetActive(false);
_isShow = 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-09-12 15:18:50 +08:00
/// 初始化前预处理
/// </summary>
2025-09-10 19:54:45 +08:00
protected virtual void OnBeforeInit(object data = null)
{
}
/// <summary>
2025-09-12 15:18:50 +08:00
/// UI初始化
/// </summary>
2025-09-12 15:18:50 +08:00
public virtual void OnInit(){}
/// <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>
/// 显示前检查
/// </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-09-12 15:18:50 +08:00
/// 显示时处理
2025-09-11 16:39:35 +08:00
/// </summary>
2025-09-12 15:18:50 +08:00
protected virtual void OnShow(object data)
2025-09-11 16:39:35 +08:00
{
}
/// <summary>
2025-09-12 15:18:50 +08:00
/// 显示后处理
/// </summary>
2025-09-12 15:18:50 +08:00
protected virtual void OnAfterShow(object data)
{
}
2025-09-12 15:18:50 +08:00
/// <summary>
2025-09-12 15:18:50 +08:00
/// 每帧更新
/// </summary>
2025-09-12 15:18:50 +08:00
protected virtual void OnUpdate(float dt)
{
}
// Refresh Phase
/// <summary>
2025-09-12 15:18:50 +08:00
/// 数据刷新
/// </summary>
2025-09-12 15:18:50 +08:00
protected virtual void OnDataRefresh(object data) { }
// Hide Phase
/// <summary>
2025-09-12 15:18:50 +08:00
/// 隐藏时处理
/// </summary>
2025-09-12 15:18:50 +08:00
protected virtual void OnHide()
{
}
2025-09-12 15:18:50 +08:00
// Release Phase
/// <summary>
2025-09-12 15:18:50 +08:00
/// 资源释放
/// </summary>
public virtual void OnRelease()
{
}
2025-09-12 15:18:50 +08:00
#endregion
}