public GameObject GetOverUI(GameObject canvas) //canvas 为当前界面canvas
{
PointerEventData pointerEventData = new PointerEventData(EventSystem.current);
#if UNITY_EDITOR
pointerEventData.position = Input.mousePosition;
#else
pointerEventData.position = touch.position;
#endif
GraphicRaycaster gr = canvas.GetComponent<GraphicRaycaster>();
List<RaycastResult> results = new List<RaycastResult>();
gr.Raycast(pointerEventData, results);
if (results.Count != 0)
{
return results[0].gameObject;
}return null;
}
判断鼠标或者手势点击在UI界面那个物体上
最新推荐文章于 2026-07-31 13:50:53 发布
这段代码在Unity环境中用于获取用户点击或触摸屏幕时所选中的GameObject。它使用GraphicRaycaster进行射线投射,根据输入设备(编辑器中的鼠标点击或运行时的触摸位置)来确定选中的游戏对象。

4531

被折叠的 条评论
为什么被折叠?



