using UnityEngine; using UnityEngine.UI; using UnityEngine.EventSystems; using System.Collections.Generic; public class TestButtonRaycast : MonoBehaviour { public GraphicRaycaster graphicRaycaster; public EventSystem eventSystem; void Update() { if (Input.GetMouseButtonDown(0)) { PointerEventData pointerData = new PointerEventData(eventSystem) { position = Input.mousePosition }; List results = new List(); graphicRaycaster.Raycast(pointerData, results); foreach (var result in results) { Debug.Log("命中物体:" + result.gameObject.name); // 若能打印按钮名称,说明射线检测正常 } } } }