437 lines
12 KiB
C#
437 lines
12 KiB
C#
using System;
|
||
using Cysharp.Threading.Tasks;
|
||
using DG.Tweening;
|
||
using Framework;
|
||
using PhxhSDK;
|
||
using UnityEngine;
|
||
using UnityEngine.UI;
|
||
|
||
/// <summary>
|
||
/// UI窗口基类 - 生命周期管理部分
|
||
/// 包含窗口的创建、显示、隐藏、关闭等生命周期管理功能
|
||
/// </summary>
|
||
public abstract partial class UIWindow
|
||
{
|
||
#region Window Lifecycle
|
||
|
||
/// <summary>
|
||
/// 尝试绑定背景关闭按钮
|
||
/// </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))
|
||
AudioManager.Instance.PlayAudio(clickAudio.AudioKey);
|
||
else
|
||
{
|
||
CommonUtilsFramework.PlayUICommonSound();
|
||
}
|
||
}
|
||
if (_onBgCloseFunc == null)
|
||
{
|
||
CloseWindow();
|
||
return;
|
||
}
|
||
_onBgCloseFunc?.Invoke();
|
||
});
|
||
}
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 创建窗口
|
||
/// </summary>
|
||
/// <param name="data">传入数据</param>
|
||
/// <param name="isNotSetCamera">是否不设置相机</param>
|
||
/// <param name="showAnimMode">显示动画模式</param>
|
||
public void CreateWindow(object data = null, bool isNotSetCamera = false,
|
||
UIManager.EShowAnimMode showAnimMode = UIManager.EShowAnimMode.Default)
|
||
{
|
||
_bDoNotSetMainCamera = isNotSetCamera;
|
||
PreLoad(data);
|
||
|
||
if (!isCreated)
|
||
{
|
||
AutoBindField();
|
||
OnInit();
|
||
InitScreenAdaption();
|
||
_TryBindBgClose();
|
||
isCreated = true;
|
||
}
|
||
|
||
_isShow = false;
|
||
|
||
if (showAnimMode == UIManager.EShowAnimMode.MustShow)
|
||
{
|
||
IsShowOpenAnim = true;
|
||
}
|
||
else if (showAnimMode == UIManager.EShowAnimMode.NotShow)
|
||
{
|
||
IsShowOpenAnim = false;
|
||
}
|
||
else
|
||
{
|
||
IsShowOpenAnim = UIManager.Instance.GetIsAutoPlayAnim(WindowName);
|
||
}
|
||
Show(data);
|
||
|
||
if (isPlayDefaultOpenAudio)
|
||
{
|
||
//AudioManager3.Instance.PlaySound("");
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 关闭窗口
|
||
/// </summary>
|
||
/// <param name="closeType">关闭类型</param>
|
||
/// <param name="forceShowAnim">强制设置动画,0不管,1强制开,-1强制不开</param>
|
||
/// <param name="isOpenMainCamera">是否打开主相机</param>
|
||
public async void CloseWindow(UIWindowCloseType closeType = UIWindowCloseType.HideAndDestroy, int forceShowAnim = 0,
|
||
bool isOpenMainCamera = true)
|
||
{
|
||
IsClosing = true;
|
||
DebugUtil.LogG($"关闭UI:{WindowName}, closeType{closeType}");
|
||
|
||
if (UIManager.Instance.CheckWindowIsFullScreen(this))
|
||
{
|
||
if (!_bDoNotSetMainCamera)
|
||
{
|
||
CameraManager.Instance.SetMainCameraActive(isOpenMainCamera);
|
||
}
|
||
}
|
||
|
||
if (isPlayDefaultCloseAudio && isCreated)
|
||
{
|
||
//AudioManager3.Instance.PlaySound("");
|
||
}
|
||
|
||
if (closeType == UIWindowCloseType.HideAndDestroy)
|
||
{
|
||
var last = UIManager.Instance.GetLast();
|
||
if (last == this)
|
||
{
|
||
DebugUtil.LogG($"PopWindow{this.WindowName}");
|
||
UIManager.Instance.PopWindow();
|
||
}
|
||
|
||
bool isShowCloseAnim = IsShowOpenAnim;
|
||
if (forceShowAnim > 0)
|
||
{
|
||
isShowCloseAnim = true;
|
||
}
|
||
|
||
if (forceShowAnim < 0)
|
||
{
|
||
isShowCloseAnim = false;
|
||
}
|
||
|
||
//调试
|
||
//isShowCloseAnim = false;
|
||
|
||
_OnClose();
|
||
if (isShowCloseAnim)
|
||
{
|
||
UIManager.Instance.OnClick(true);
|
||
await AnimHide(() =>
|
||
{
|
||
DestroyWindow();
|
||
UIManager.Instance.RemoveWindow(mWindowName);
|
||
IsClosing = false;
|
||
});
|
||
}
|
||
else
|
||
{
|
||
UIManager.Instance.RemoveWindow(mWindowName);
|
||
DestroyWindow();
|
||
IsClosing = false;
|
||
}
|
||
}
|
||
else if(closeType == UIWindowCloseType.OnlyHide)
|
||
{
|
||
Hide();
|
||
IsClosing = false;
|
||
}
|
||
else if(closeType == UIWindowCloseType.HideAndPush)
|
||
{
|
||
Hide();
|
||
IsClosing = false;
|
||
//UIManager.Instance.PushWindow(this);
|
||
}
|
||
else
|
||
{
|
||
//强制清除所有界面时使用
|
||
DestroyWindow();
|
||
UIManager.Instance.RemoveWindow(mWindowName);
|
||
IsClosing = false;
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 重新加载窗口
|
||
/// </summary>
|
||
/// <param name="data">传入数据</param>
|
||
public void ReloadWindow(object data)
|
||
{
|
||
if (isCreated)
|
||
{
|
||
OnReloadWindow(data);
|
||
return;
|
||
}
|
||
|
||
Show(data);
|
||
OnReloadWindow(data);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 显示窗口
|
||
/// </summary>
|
||
/// <param name="data">传入数据</param>
|
||
private async void Show(object data)
|
||
{
|
||
//未被唤醒不执行Show
|
||
if (IsUnEnabled)
|
||
{
|
||
return;
|
||
}
|
||
|
||
if (gameObject == null)
|
||
{
|
||
DebugUtil.Log("UI已被销毁:{0}", WindowName);
|
||
}
|
||
else
|
||
{
|
||
gameObject.SetActive(true);
|
||
}
|
||
|
||
if (UIManager.Instance.WindowInfos.TryGetValue(WindowName, out var windowInfo))
|
||
{
|
||
if (CameraManager.Instance.CheckMainCameraIsActive())
|
||
{
|
||
if (windowInfo.UIOpenType == UIOpenType.FullScreen)
|
||
{
|
||
//特殊处理主界面
|
||
if (!_bDoNotSetMainCamera && WindowName != UINameConst.UI_MainPanel)
|
||
{
|
||
CameraManager.Instance.SetMainCameraActive(false);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
OnShowWindow(data);
|
||
_isShow = true;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 重新显示窗口
|
||
/// </summary>
|
||
private void ShowAgain()
|
||
{
|
||
if (gameObject)
|
||
{
|
||
gameObject.SetActive(true);
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 隐藏窗口
|
||
/// </summary>
|
||
/// <param name="isOnlyHide">是否仅隐藏</param>
|
||
private void Hide(bool isOnlyHide = false)
|
||
{
|
||
gameObject?.SetActive(false);
|
||
|
||
if (!isOnlyHide)
|
||
{
|
||
OnHideWindow();
|
||
_isShow = false;
|
||
|
||
if (UIManager.Instance.WindowInfos.TryGetValue(WindowName, out var windowInfo))
|
||
{
|
||
if (windowInfo.UIOpenType == UIOpenType.FullScreen)
|
||
{
|
||
if (!CameraManager.Instance.CheckMainCameraIsActive())
|
||
{
|
||
CameraManager.Instance.SetMainCameraActive(true);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 关闭时的内部处理
|
||
/// </summary>
|
||
private void _OnClose()
|
||
{
|
||
if (UIManager.Instance.UiWindowStack.Count <= 0)
|
||
{
|
||
return;
|
||
}
|
||
|
||
if (UIManager.Instance.CheckWindowIsFullScreen(this))
|
||
{
|
||
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--)
|
||
{
|
||
var latestWindow = UIManager.Instance.UiWindowStack[i];
|
||
latestWindow.ShowWindow();
|
||
|
||
if (UIManager.Instance.CheckWindowIsFullScreen(latestWindow))
|
||
break;
|
||
}
|
||
}
|
||
else
|
||
{
|
||
UIManager.Instance.ForceRemoveWindow(this);
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 销毁窗口
|
||
/// </summary>
|
||
private void DestroyWindow()
|
||
{
|
||
DebugUtil.LogG($"DestroyWindow:{WindowName}");
|
||
OnHideWindow();
|
||
|
||
if (gameObject)
|
||
{
|
||
if (isCreated)
|
||
{
|
||
OnRelease();
|
||
isCreated = false;
|
||
}
|
||
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;
|
||
}
|
||
|
||
#endregion
|
||
|
||
#region Public Lifecycle Interface
|
||
|
||
/// <summary>
|
||
/// 隐藏窗口(公共接口)
|
||
/// </summary>
|
||
/// <param name="isOnlyHide">是否仅隐藏</param>
|
||
public void HideWindow(bool isOnlyHide = false)
|
||
{
|
||
Hide(isOnlyHide);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 激活显示窗口(公共接口)
|
||
/// </summary>
|
||
public void ShowWindowActive()
|
||
{
|
||
ShowAgain();
|
||
}
|
||
|
||
/// <summary>
|
||
/// 显示窗口(公共接口)
|
||
/// </summary>
|
||
/// <param name="data">传入数据</param>
|
||
public void ShowWindow(object data = null)
|
||
{
|
||
Show(data);
|
||
}
|
||
|
||
#endregion
|
||
|
||
#region Lifecycle Callbacks
|
||
|
||
/// <summary>
|
||
/// 预加载,传入参数,不能添加监听
|
||
/// </summary>
|
||
/// <param name="data">传入数据</param>
|
||
public virtual void PreLoad(object data = null)
|
||
{
|
||
|
||
}
|
||
|
||
/// <summary>
|
||
/// 打开界面时调用
|
||
/// </summary>
|
||
/// <param name="data">传入数据</param>
|
||
protected virtual void OnShowWindow(object data)
|
||
{
|
||
EventManager.Instance.Send(EventManager.EventName.UIOpen, WindowName);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 关闭界面时调用
|
||
/// </summary>
|
||
protected virtual void OnHideWindow()
|
||
{
|
||
EventManager.Instance.Send(EventManager.EventName.UIClose, WindowName);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 重新加载界面时调用
|
||
/// </summary>
|
||
/// <param name="data">传入数据</param>
|
||
protected virtual void OnReloadWindow(object data) { }
|
||
|
||
/// <summary>
|
||
/// 私有Awake方法,会在基类Awake执行后调用
|
||
/// </summary>
|
||
public virtual void OnInit(){}
|
||
|
||
/// <summary>
|
||
/// 自动绑定字段
|
||
/// </summary>
|
||
protected virtual void AutoBindField()
|
||
{
|
||
}
|
||
|
||
/// <summary>
|
||
/// 对应OnInit,在销毁掉界面时调用
|
||
/// </summary>
|
||
public virtual void OnRelease()
|
||
{
|
||
EventManager.Instance.Send(EventManager.EventName.UIDestroy, WindowName);
|
||
|
||
UnLoad();
|
||
_openAnimSeq?.Kill();
|
||
|
||
// 清理资源引用管理器
|
||
_assetReferenceManager?.Dispose();
|
||
_assetReferenceManager = null;
|
||
|
||
// 清理刘海屏适配器
|
||
_notchScreenAdapter = null;
|
||
|
||
// 清理GraphicRaycaster管理器
|
||
_graphicRaycasterManager = null;
|
||
}
|
||
|
||
#endregion
|
||
}
|