using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class UITest : MonoBehaviour
{
void Update()
{
if (IsPointerOverGameObject(Input.mousePosition))
{
// Debug.Log("点击到UI");
}
}
private bool IsPointerOverGameObject(Vector2 mousePosition)
{
//创建一个点击事件
PointerEventData eventData = new PointerEventData(UnityEngine.EventSystems.EventSystem.current);
eventData.position = mousePosition;
List<RaycastResult> raycastResults = new List<RaycastResult>();
//向点击位置发射一条射线,检测是否点击UI
UnityEngine.EventSystems.EventSystem.current.RaycastAll(eventData, raycastResults);
if (raycastResults.Count > 0)
{
Debug.Log(raycastResults[0].gameObject.name);
return true;
}
else
{
return false;
}
}
}
【Unity】判断鼠标是否点击在UI上
于 2022-08-08 14:27:49 首次发布