109 lines
2.8 KiB
C#
109 lines
2.8 KiB
C#
using UnityEngine;
|
||
using UnityEngine.UI;
|
||
|
||
/// <summary>
|
||
/// GraphicRaycaster 管理器,负责管理UI的射线检测功能
|
||
/// 使用组合模式,从UIWindow中提取出GraphicRaycaster管理功能
|
||
/// </summary>
|
||
public class GraphicRaycasterManager
|
||
{
|
||
private GameObject _rootGameObject;
|
||
private int _disableGraphicRaycasterCount;
|
||
|
||
/// <summary>
|
||
/// 构造函数
|
||
/// </summary>
|
||
/// <param name="rootGameObject">需要管理GraphicRaycaster的根GameObject</param>
|
||
public GraphicRaycasterManager(GameObject rootGameObject)
|
||
{
|
||
_rootGameObject = rootGameObject;
|
||
_disableGraphicRaycasterCount = 0;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 是否启用GraphicRaycaster
|
||
/// </summary>
|
||
public bool IsEnableGraphicRaycaster
|
||
{
|
||
get { return _disableGraphicRaycasterCount <= 0; }
|
||
set
|
||
{
|
||
if (value)
|
||
{
|
||
if (_disableGraphicRaycasterCount > 0)
|
||
--_disableGraphicRaycasterCount;
|
||
}
|
||
else
|
||
++_disableGraphicRaycasterCount;
|
||
|
||
if (null == _rootGameObject)
|
||
return;
|
||
|
||
UpdateGraphicRaycasterState();
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 更新GraphicRaycaster状态
|
||
/// </summary>
|
||
private void UpdateGraphicRaycasterState()
|
||
{
|
||
bool isEnabled = _disableGraphicRaycasterCount <= 0;
|
||
|
||
if (null == _rootGameObject)
|
||
return;
|
||
|
||
// 确保根对象有GraphicRaycaster组件
|
||
if (!_rootGameObject.TryGetComponent(out GraphicRaycaster graphicRaycaster))
|
||
graphicRaycaster = _rootGameObject.AddComponent<GraphicRaycaster>();
|
||
|
||
if (null != graphicRaycaster)
|
||
graphicRaycaster.enabled = isEnabled;
|
||
|
||
// 更新所有子对象的GraphicRaycaster
|
||
GraphicRaycaster[] graphicRaycasters = _rootGameObject.GetComponentsInChildren<GraphicRaycaster>();
|
||
foreach (GraphicRaycaster gr in graphicRaycasters)
|
||
{
|
||
if (null == gr)
|
||
continue;
|
||
|
||
gr.enabled = isEnabled;
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 获取当前禁用计数
|
||
/// </summary>
|
||
/// <returns>禁用计数</returns>
|
||
public int GetDisableCount()
|
||
{
|
||
return _disableGraphicRaycasterCount;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 重置禁用计数
|
||
/// </summary>
|
||
public void ResetDisableCount()
|
||
{
|
||
_disableGraphicRaycasterCount = 0;
|
||
UpdateGraphicRaycasterState();
|
||
}
|
||
|
||
/// <summary>
|
||
/// 强制启用GraphicRaycaster
|
||
/// </summary>
|
||
public void ForceEnable()
|
||
{
|
||
_disableGraphicRaycasterCount = 0;
|
||
UpdateGraphicRaycasterState();
|
||
}
|
||
|
||
/// <summary>
|
||
/// 强制禁用GraphicRaycaster
|
||
/// </summary>
|
||
public void ForceDisable()
|
||
{
|
||
_disableGraphicRaycasterCount = 1;
|
||
UpdateGraphicRaycasterState();
|
||
}
|
||
} |