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

254 lines
6.2 KiB
C#
Raw Blame History

This file contains invisible Unicode characters!

This file contains invisible Unicode characters that may be processed differently from what appears below. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to reveal hidden characters.

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

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;
public delegate void OnBgClose();
private OnBgClose _onBgCloseFunc;
/// <summary>
/// 资源引用管理器
/// </summary>
private PhxhSDK.AssetReferenceManager _assetReferenceManager;
private bool _shutMainCamera = true;
private bool _isCloseCbCalled;
protected bool _isShow = false;
// 刘海屏适配器Ω
private UINotchScreenAdapter _notchScreenAdapter;
// GraphicRaycaster管理器
private GraphicRaycasterManager _graphicRaycasterManager;
/// <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;
}
}
public bool IsClosing; //正在关闭
// 是否播放默认的打开对话框的声音
public bool isPlayDefaultOpenAudio = true;
// 是否播放默认的关闭对话框的声音
public bool isPlayDefaultCloseAudio = true;
private bool _isPlayingOpenAnim;
protected Sequence _openAnimSeq;
public bool IsShowOpenAnim;
public string WindowName
{
set { mWindowName = value; }
get { return mWindowName; }
}
[HideInInspector]
public UIWindowLayer uiWindowLayer;
[HideInInspector]
protected bool isInited = false;
/// <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);
}
}
public async UniTask<T> LoadAssetAsync<T>(string path) where T : UnityEngine.Object
{
if (gameObject)
{
// 确保资源管理器已初始化
if (_assetReferenceManager == null)
{
_assetReferenceManager = new PhxhSDK.AssetReferenceManager();
}
return await _assetReferenceManager.LoadAssetAsync<T>(path);
}
// 如果gameObject为空直接通过AssetManager加载
return await AssetManager.Instance.LoadAssetAsync<T>(path);
}
/// <summary>
/// 异步预加载资源
/// </summary>
/// <param name="path">资源路径</param>
public async UniTask PostPreload<T>(string path) where T : UnityEngine.Object
{
// 确保资源管理器已初始化
if (_assetReferenceManager == null)
{
_assetReferenceManager = new PhxhSDK.AssetReferenceManager();
}
await _assetReferenceManager.PostPreload<T>(path);
}
/// <summary>
/// 获取预加载的资源结果
/// </summary>
/// <param name="path">资源路径</param>
public T GetPreLoadResult<T>(string path) where T : UnityEngine.Object
{
// 优先使用资源管理器获取结果
if (_assetReferenceManager != null)
{
return _assetReferenceManager.GetPreLoadResult<T>(path);
}
// 回退到直接使用AssetManager
return AssetManager.Instance.GetPreLoadResult<T>(path);
}
protected void UnLoad()
{
// 使用资源管理器卸载所有资源
if (_assetReferenceManager != null)
{
_assetReferenceManager.UnloadAll();
}
}
public virtual async UniTask<bool> OnBack(object data = null)
{
if (!GraphicRaycasterManager.IsEnableGraphicRaycaster)
return true;
if (UIManager.Instance.GetLast() != null)
{
OnDirectBack();
return true;
}
await OnBackApp();
return true;
}
protected void OnDirectBack()
{
CloseWindow(UIWindowCloseType.HideAndDestroy);
}
protected async UniTask OnBackApp()
{
await UIManager.Instance.CreateAndOpenWindow(UINameConst.UIBackReturn);
}
#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<T>(string path = "") where T : Behaviour
{
var component = transform.Find(path)?.GetComponent<T>();
if (component is null)
{
DebugUtil.LogError("{0}未找到组件{1} {2}", mWindowName, typeof(T).Name, path);
}
return component;
}
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;
}
#endregion
#region State Query Methods
public bool IsCreated()
{
return isInited;
}
public bool IsShow()
{
return _isShow;
}
public bool IsHide()
{
return !_isShow;
}
#endregion
protected void AddBgClose(OnBgClose onBgCloseFunc)
{
_onBgCloseFunc += onBgCloseFunc;
}
protected virtual void OnCloseClick()
{
}
}