using Cysharp.Threading.Tasks;
using DG.Tweening;
using PhxhSDK;
using UnityEngine;
using UniTask = Cysharp.Threading.Tasks.UniTask;
public abstract partial class UIWindow
{
public GameObject gameObject;
public Transform transform;
public string name;
private string mWindowName;
///
/// 资源引用管理器
///
private PhxhSDK.AssetReferenceManager _assetReferenceManager;
private bool _isCloseCbCalled;
protected bool _isShow = false;
protected bool _isPaused = false; // 窗口暂停状态(用于音乐、动画等逻辑控制)
// 刘海屏适配器Ω
private UINotchScreenAdapter _notchScreenAdapter;
// GraphicRaycaster管理器
private GraphicRaycasterManager _graphicRaycasterManager;
///
/// Public accessor for the `GraphicRaycasterManager`. Lazily initializes on first access and cannot be assigned.
///
public GraphicRaycasterManager GraphicRaycasterManager
{
get
{
if (_graphicRaycasterManager == null)
InitGraphicRaycasterManager();
return _graphicRaycasterManager;
}
}
public bool IsClosing; //正在关闭
///
/// 返回键策略(在窗口加载到Layer时设置)
///
public IUIBackStrategy BackStrategy { get; set; }
private bool _isPlayingOpenAnim;
protected Sequence _openAnimSeq;
public bool IsShowOpenAnim;
public string WindowName
{
set { mWindowName = value; }
get { return mWindowName; }
}
[HideInInspector]
public UICanvasLayer uiWindowLayer;
[HideInInspector]
protected bool isInited = false;
///
/// 初始化GraphicRaycaster管理器
///
private void InitGraphicRaycasterManager()
{
if (_graphicRaycasterManager == null && gameObject is not null)
{
_graphicRaycasterManager = new GraphicRaycasterManager(gameObject);
}
}
///
/// 初始化刘海屏适配
///
private void InitScreenAdaption()
{
if (_notchScreenAdapter == null)
{
_notchScreenAdapter = new UINotchScreenAdapter(transform);
}
}
public async UniTask LoadAssetAsync(string path) where T : UnityEngine.Object
{
if (gameObject)
{
// 确保资源管理器已初始化
if (_assetReferenceManager == null)
{
_assetReferenceManager = new PhxhSDK.AssetReferenceManager();
}
return await _assetReferenceManager.LoadAssetAsync(path);
}
// 如果gameObject为空,直接通过AssetManager加载
return await AssetManager.Instance.LoadAssetAsync(path);
}
///
/// 异步预加载资源
///
/// 资源路径
public async UniTask PostPreload(string path) where T : UnityEngine.Object
{
// 确保资源管理器已初始化
if (_assetReferenceManager == null)
{
_assetReferenceManager = new PhxhSDK.AssetReferenceManager();
}
await _assetReferenceManager.PostPreload(path);
}
///
/// 获取预加载的资源结果
///
/// 资源路径
public T GetPreLoadResult(string path) where T : UnityEngine.Object
{
// 优先使用资源管理器获取结果
if (_assetReferenceManager != null)
{
return _assetReferenceManager.GetPreLoadResult(path);
}
// 回退到直接使用AssetManager
return AssetManager.Instance.GetPreLoadResult(path);
}
protected void UnLoad()
{
// 使用资源管理器卸载所有资源
if (_assetReferenceManager != null)
{
_assetReferenceManager.UnloadAll();
}
}
#region Component Utilities
public GameObject FindObj(string path)
{
GameObject obj = transform.Find(path)?.gameObject;
if (obj is null)
{
DebugUtil.LogError("{0}未找到 {1}/{2}", mWindowName, gameObject.name, path);
}
return obj;
}
public T GetComponent(string path = "") where T : Behaviour
{
var component = transform.Find(path)?.GetComponent();
if (component is null)
{
DebugUtil.LogError("{0}未找到组件{1}: {2}", mWindowName, typeof(T).Name, path);
}
return component;
}
public T AddComponent(string path = "") where T : Behaviour
{
var trans = transform.Find(path);
if (trans)
{
var component = trans.GetComponent();
if (component is null)
{
component = trans.gameObject.AddComponent();
//DebugUtil.LogError("{0}未找到组件{1}: {2}", mWindowName, typeof(T).Name, path);
}
return component;
}
DebugUtil.LogError("{0}未找到路径: {1}", mWindowName, path);
return null;
}
#endregion
#region State Query Methods
public bool IsCreated()
{
return isInited;
}
public bool IsShow()
{
return _isShow;
}
public bool IsHide()
{
return !_isShow;
}
///
/// 是否处于活跃状态(显示且未暂停)
///
public bool IsActive()
{
return _isShow && !_isPaused;
}
///
/// 是否处于暂停状态
///
public bool IsPaused()
{
return _isPaused;
}
#endregion
#region Pause/Resume Management
///
/// 暂停窗口(手动调用)
///
/// 功能:
/// - 标记窗口为暂停状态
/// - 调用 OnPause 回调
/// - 可选:隐藏渲染(通过 hideRendering 参数控制)
///
/// 使用场景:
/// - 游戏暂停时,手动暂停战斗 UI
/// - 弹出全屏广告时,暂停游戏主界面
/// - 特殊业务逻辑需要暂停某个窗口
///
/// 示例:
/// battleUI.PauseWindow(hideRendering: false); // 暂停逻辑但保持显示
///
/// 是否同时隐藏渲染(SetActive(false))
public void PauseWindow(bool hideRendering = true)
{
// 状态检查
if (!_isShow)
{
DebugUtil.LogWarning($"[UIWindow] PauseWindow failed: window is not showing ({WindowName})");
return;
}
if (_isPaused)
{
DebugUtil.LogWarning($"[UIWindow] PauseWindow ignored: already paused ({WindowName})");
return;
}
_isPaused = true;
// 调用子类的暂停逻辑
OnPause();
// 可选:隐藏渲染
if (hideRendering && gameObject != null && gameObject.activeSelf)
{
gameObject.SetActive(false);
DebugUtil.Log($"[UIWindow] Paused (hidden): {WindowName}");
}
else
{
DebugUtil.Log($"[UIWindow] Paused (logic only): {WindowName}");
}
}
///
/// 恢复窗口(手动调用)
///
/// 功能:
/// - 标记窗口为活跃状态
/// - 调用 OnResume 回调
/// - 可选:显示渲染(通过 showRendering 参数控制)
///
/// 使用场景:
/// - 游戏恢复时,手动恢复战斗 UI
/// - 广告关闭后,恢复游戏主界面
/// - 特殊业务逻辑需要恢复某个窗口
///
/// 示例:
/// battleUI.ResumeWindow(showRendering: true); // 恢复逻辑并显示
///
/// 是否同时显示渲染(SetActive(true))
public void ResumeWindow(bool showRendering = true)
{
// 状态检查
if (!_isShow)
{
DebugUtil.LogWarning($"[UIWindow] ResumeWindow failed: window is not showing ({WindowName})");
return;
}
if (!_isPaused)
{
DebugUtil.LogWarning($"[UIWindow] ResumeWindow ignored: not paused ({WindowName})");
return;
}
_isPaused = false;
// 可选:显示渲染
if (showRendering && gameObject != null && !gameObject.activeSelf)
{
gameObject.SetActive(true);
DebugUtil.Log($"[UIWindow] Resumed (shown): {WindowName}");
}
else
{
DebugUtil.Log($"[UIWindow] Resumed (logic only): {WindowName}");
}
// 调用子类的恢复逻辑
OnResume();
}
#endregion
#region Virtual Methods - Pause/Resume
///
/// 窗口暂停回调(子类重写)
///
/// 调用时机:手动调用 PauseWindow() 时
///
/// 建议处理:
/// - 停止播放背景音乐/音效
/// - 暂停动画/粒子特效
/// - 暂停定时器/倒计时
/// - 暂停网络轮询(可选)
///
/// 示例:
///
/// protected override void OnPause()
/// {
/// if (_bgmHandle != -1)
/// AudioManager.Instance.PauseBGM(_bgmHandle);
/// }
///
///
protected virtual void OnPause() { }
///
/// 窗口恢复回调(子类重写)
///
/// 调用时机:手动调用 ResumeWindow() 时
///
/// 建议处理:
/// - 恢复播放背景音乐/音效
/// - 恢复动画/粒子特效
/// - 恢复定时器/倒计时
/// - 刷新数据(可选)
///
/// 示例:
///
/// protected override void OnResume()
/// {
/// if (_bgmHandle != -1)
/// AudioManager.Instance.ResumeBGM(_bgmHandle);
/// }
///
///
protected virtual void OnResume() { }
#endregion
}