133 lines
4.2 KiB
C#
133 lines
4.2 KiB
C#
|
|
|
|||
|
|
using PhxhSDK;
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// UI窗口相机管理器
|
|||
|
|
///
|
|||
|
|
/// 核心理念:
|
|||
|
|
/// - 根据窗口栈状态智能管理主相机的开关
|
|||
|
|
/// - 当最上层窗口关闭时,检查下层是否还有需要关闭相机的窗口
|
|||
|
|
/// - 只有当所有需要关闭相机的窗口都关闭后,才重新打开相机
|
|||
|
|
///
|
|||
|
|
/// 管理目标:
|
|||
|
|
/// - 避免相机在多层UI之间频繁开关
|
|||
|
|
/// - 保证相机状态与UI栈状态一致
|
|||
|
|
/// - 处理时机在 UIWindowVisibilityManager 之后
|
|||
|
|
/// </summary>
|
|||
|
|
public class UIWindowCameraManager
|
|||
|
|
{
|
|||
|
|
#region Singleton
|
|||
|
|
private static UIWindowCameraManager _instance;
|
|||
|
|
public static UIWindowCameraManager Instance
|
|||
|
|
{
|
|||
|
|
get
|
|||
|
|
{
|
|||
|
|
if (_instance == null)
|
|||
|
|
{
|
|||
|
|
_instance = new UIWindowCameraManager();
|
|||
|
|
}
|
|||
|
|
return _instance;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private UIWindowCameraManager() { }
|
|||
|
|
#endregion
|
|||
|
|
|
|||
|
|
#region Camera Operations
|
|||
|
|
/// <summary>
|
|||
|
|
/// 处理窗口打开时的相机状态
|
|||
|
|
/// 如果窗口需要关闭相机,则关闭主相机
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="newWindow">新打开的窗口</param>
|
|||
|
|
public void HandleCameraOnOpen(UIWindow newWindow)
|
|||
|
|
{
|
|||
|
|
if (newWindow == null)
|
|||
|
|
return;
|
|||
|
|
|
|||
|
|
var config = UIManager.Instance.GetWindowInfo(newWindow.WindowName);
|
|||
|
|
|
|||
|
|
// 如果窗口需要关闭主相机,则关闭它
|
|||
|
|
if (config.ShutMainCamera)
|
|||
|
|
{
|
|||
|
|
if (CameraManager.Instance.CheckMainCameraIsActive())
|
|||
|
|
{
|
|||
|
|
CameraManager.Instance.SetMainCameraActive(false);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 处理窗口关闭时的相机恢复
|
|||
|
|
/// 检查下层是否还有需要关闭相机的窗口
|
|||
|
|
/// - 如果下层还有 ShutMainCamera 的窗口,则不打开相机
|
|||
|
|
/// - 如果下层没有 ShutMainCamera 的窗口,则需要打开相机
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="closingWindow">正在关闭的窗口</param>
|
|||
|
|
public void HandleCameraOnClose(UIWindow closingWindow)
|
|||
|
|
{
|
|||
|
|
if (closingWindow == null)
|
|||
|
|
return;
|
|||
|
|
|
|||
|
|
var config = UIManager.Instance.GetWindowInfo(closingWindow.WindowName);
|
|||
|
|
|
|||
|
|
// 只有关闭的窗口本身需要关闭相机时,才需要检查是否要恢复相机
|
|||
|
|
if (!config.ShutMainCamera)
|
|||
|
|
return;
|
|||
|
|
|
|||
|
|
// 检查是否还有其他需要关闭相机的窗口在显示
|
|||
|
|
bool hasOtherShutCameraWindow = CheckHasShutCameraWindowInStack(closingWindow);
|
|||
|
|
|
|||
|
|
// 如果没有其他需要关闭相机的窗口,且相机当前是关闭状态,则打开相机
|
|||
|
|
if (!hasOtherShutCameraWindow)
|
|||
|
|
{
|
|||
|
|
if (!CameraManager.Instance.CheckMainCameraIsActive())
|
|||
|
|
{
|
|||
|
|
CameraManager.Instance.SetMainCameraActive(true);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
#endregion
|
|||
|
|
|
|||
|
|
#region Helper Methods
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 检查窗口栈中是否还有其他需要关闭相机的窗口
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="excludeWindow">要排除的窗口(正在关闭的窗口)</param>
|
|||
|
|
/// <returns>是否存在其他需要关闭相机的窗口</returns>
|
|||
|
|
private bool CheckHasShutCameraWindowInStack(UIWindow excludeWindow)
|
|||
|
|
{
|
|||
|
|
var layerManager = UICanvasLayerManager.Instance;
|
|||
|
|
|
|||
|
|
// 从最高层开始遍历所有层级
|
|||
|
|
for (int layer = (int)UICanvasLayer.Max - 1; layer >= 0; layer--)
|
|||
|
|
{
|
|||
|
|
var windowList = layerManager.GetWindowsInLayer((UICanvasLayer)layer);
|
|||
|
|
|
|||
|
|
foreach (var window in windowList)
|
|||
|
|
{
|
|||
|
|
// 跳过正在关闭的窗口
|
|||
|
|
if (window == null || window == excludeWindow)
|
|||
|
|
continue;
|
|||
|
|
|
|||
|
|
// 只检查逻辑上显示的窗口(IsShow=true)
|
|||
|
|
if (!window.IsShow())
|
|||
|
|
continue;
|
|||
|
|
|
|||
|
|
// 检查该窗口是否需要关闭相机
|
|||
|
|
var windowConfig = UIManager.Instance.GetWindowInfo(window.WindowName);
|
|||
|
|
if (windowConfig.ShutMainCamera)
|
|||
|
|
{
|
|||
|
|
return true;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return false;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
#endregion
|
|||
|
|
}
|
|||
|
|
|