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

208 lines
5.3 KiB
C#
Raw Normal View History

using Cysharp.Threading.Tasks;
2024-08-12 19:31:50 +08:00
using DG.Tweening;
2023-10-19 15:00:19 +08:00
using PhxhSDK;
2023-08-22 19:08:45 +08:00
using UnityEngine;
2024-05-10 19:18:40 +08:00
using UniTask = Cysharp.Threading.Tasks.UniTask;
2023-08-22 19:08:45 +08:00
2025-09-03 19:12:26 +08:00
public abstract partial class UIWindow
2023-08-22 19:08:45 +08:00
{
2024-01-26 16:06:42 +08:00
public GameObject gameObject;
public Transform transform;
public string name;
2023-08-22 19:08:45 +08:00
private string mWindowName;
2025-09-03 18:19:04 +08:00
/// <summary>
/// 资源引用管理器
/// </summary>
private PhxhSDK.AssetReferenceManager _assetReferenceManager;
2025-12-29 20:02:22 +08:00
private bool _isCloseCbCalled;
protected bool _isShow = false;
// 刘海屏适配器Ω
private UINotchScreenAdapter _notchScreenAdapter;
// GraphicRaycaster管理器
private GraphicRaycasterManager _graphicRaycasterManager;
2025-08-14 15:00:57 +08:00
/// <summary>
/// Public accessor for the `GraphicRaycasterManager`. Lazily initializes on first access and cannot be assigned.
/// </summary>
public GraphicRaycasterManager GraphicRaycasterManager
{
get
{
if (_graphicRaycasterManager == null)
InitGraphicRaycasterManager();
return _graphicRaycasterManager;
}
}
2025-04-16 12:24:10 +08:00
public bool IsClosing; //正在关闭
2025-12-26 12:16:37 +08:00
2025-12-25 20:26:05 +08:00
/// <summary>
/// 返回键策略在窗口加载到Layer时设置
/// </summary>
public IUIBackStrategy BackStrategy { get; set; }
2024-08-12 19:31:50 +08:00
private bool _isPlayingOpenAnim;
protected Sequence _openAnimSeq;
2024-08-12 19:31:50 +08:00
public bool IsShowOpenAnim;
2023-08-22 19:08:45 +08:00
public string WindowName
{
set { mWindowName = value; }
get { return mWindowName; }
}
2023-11-21 13:31:06 +08:00
2023-08-22 19:08:45 +08:00
[HideInInspector]
public UICanvasLayer uiWindowLayer;
2023-08-22 19:08:45 +08:00
2023-10-19 15:00:19 +08:00
[HideInInspector]
2025-09-09 20:26:41 +08:00
protected bool isInited = false;
2024-04-29 15:39:46 +08:00
/// <summary>
/// 初始化GraphicRaycaster管理器
/// </summary>
private void InitGraphicRaycasterManager()
{
if (_graphicRaycasterManager == null && gameObject is not null)
{
_graphicRaycasterManager = new GraphicRaycasterManager(gameObject);
}
}
/// <summary>
/// 初始化刘海屏适配
/// </summary>
private void InitScreenAdaption()
{
if (_notchScreenAdapter == null)
{
_notchScreenAdapter = new UINotchScreenAdapter(transform);
}
}
2024-05-08 16:16:50 +08:00
public async UniTask<T> LoadAssetAsync<T>(string path) where T : UnityEngine.Object
{
if (gameObject)
2024-05-08 12:44:22 +08:00
{
2025-09-03 18:19:04 +08:00
// 确保资源管理器已初始化
if (_assetReferenceManager == null)
{
2025-09-03 18:19:04 +08:00
_assetReferenceManager = new PhxhSDK.AssetReferenceManager();
}
2025-09-03 18:19:04 +08:00
return await _assetReferenceManager.LoadAssetAsync<T>(path);
}
2025-09-03 18:19:04 +08:00
// 如果gameObject为空直接通过AssetManager加载
return await AssetManager.Instance.LoadAssetAsync<T>(path);
}
/// <summary>
2025-09-09 20:26:41 +08:00
/// 异步预加载资源
/// </summary>
2025-09-09 20:26:41 +08:00
/// <param name="path">资源路径</param>
public async UniTask PostPreload<T>(string path) where T : UnityEngine.Object
{
2025-09-03 18:19:04 +08:00
// 确保资源管理器已初始化
if (_assetReferenceManager == null)
{
_assetReferenceManager = new PhxhSDK.AssetReferenceManager();
}
await _assetReferenceManager.PostPreload<T>(path);
}
2025-09-09 20:26:41 +08:00
/// <summary>
/// 获取预加载的资源结果
/// </summary>
/// <param name="path">资源路径</param>
public T GetPreLoadResult<T>(string path) where T : UnityEngine.Object
{
2025-09-03 18:19:04 +08:00
// 优先使用资源管理器获取结果
if (_assetReferenceManager != null)
{
return _assetReferenceManager.GetPreLoadResult<T>(path);
}
// 回退到直接使用AssetManager
return AssetManager.Instance.GetPreLoadResult<T>(path);
}
protected void UnLoad()
{
2025-09-03 18:19:04 +08:00
// 使用资源管理器卸载所有资源
if (_assetReferenceManager != null)
{
2025-09-03 18:19:04 +08:00
_assetReferenceManager.UnloadAll();
}
}
2024-05-09 14:37:29 +08:00
#region Component Utilities
2023-08-22 19:08:45 +08:00
public GameObject FindObj(string path)
{
GameObject obj = transform.Find(path)?.gameObject;
if (obj is null)
2023-10-19 15:00:19 +08:00
{
DebugUtil.LogError("{0}未找到 {1}/{2}", mWindowName, gameObject.name, path);
}
2023-08-22 19:08:45 +08:00
return obj;
}
2024-01-26 16:06:42 +08:00
public T GetComponent<T>(string path = "") where T : Behaviour
2023-08-22 19:08:45 +08:00
{
2023-10-19 15:00:19 +08:00
var component = transform.Find(path)?.GetComponent<T>();
if (component is null)
2023-10-19 15:00:19 +08:00
{
DebugUtil.LogError("{0}未找到组件{1} {2}", mWindowName, typeof(T).Name, path);
}
return component;
2023-08-22 19:08:45 +08:00
}
public T AddComponent<T>(string path = "") where T : Behaviour
{
var trans = transform.Find(path);
if (trans)
{
var component = trans.GetComponent<T>();
if (component is null)
{
component = trans.gameObject.AddComponent<T>();
//DebugUtil.LogError("{0}未找到组件{1} {2}", mWindowName, typeof(T).Name, path);
}
return component;
}
DebugUtil.LogError("{0}未找到路径: {1}", mWindowName, path);
return null;
}
2024-01-24 15:42:01 +08:00
#endregion
#region State Query Methods
public bool IsCreated()
2023-12-29 15:03:59 +08:00
{
2025-09-09 20:26:41 +08:00
return isInited;
2023-12-29 15:03:59 +08:00
}
public bool IsShow()
2023-08-22 19:08:45 +08:00
{
return _isShow;
2023-08-22 19:08:45 +08:00
}
public bool IsHide()
{
return !_isShow;
}
#endregion
2023-08-22 19:08:45 +08:00
}