unity里鼠标位置是否在物体上。
创始人
2024-11-16 07:06:42

1. 使用Raycast

如果你的图片是在UI Canvas上,可以使用Raycast来检测鼠标点击是否在图片上。

using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class ImageClickChecker : MonoBehaviour
{
    public Image targetImage;

    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            if (IsPointerOverUIObject(targetImage.gameObject))
            {
                Debug.Log("Mouse is over the image!");
            }
            else
            {
                Debug.Log("Mouse is not over the image.");
            }
        }
    }

    private bool IsPointerOverUIObject(GameObject target)
    {
        PointerEventData pointerEventData = new PointerEventData(EventSystem.current);
        pointerEventData.position = Input.mousePosition;

        List results = new List();
        EventSystem.current.RaycastAll(pointerEventData, results);

        foreach (RaycastResult result in results)
        {
            if (result.gameObject == target)
            {
                return true;
            }
        }
        return false;
    }
}
 

2. 使用RectTransform Utility

另外一种方法是直接使用RectTransformUtility来进行坐标转换和检测。

using UnityEngine;
using UnityEngine.UI;

public class ImageClickChecker : MonoBehaviour
{
    public Image targetImage;

    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            if (IsMouseOverImage(targetImage))
            {
                Debug.Log("Mouse is over the image!");
            }
            else
            {
                Debug.Log("Mouse is not over the image.");
            }
        }
    }

    private bool IsMouseOverImage(Image image)
    {
        RectTransform rectTransform = image.GetComponent();
        Vector2 localMousePosition = RectTransformUtility.ScreenPointToLocalPointInRectangle(
            rectTransform, Input.mousePosition, null, out Vector2 localPoint);
        return rectTransform.rect.Contains(localPoint);
    }
}
 

3. 使用Collider和Physics Raycast

如果你的图片是3D对象,使用Collider和Physics Raycast可以更容易实现。

using UnityEngine;

public class ImageClickChecker : MonoBehaviour
{
    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;

            if (Physics.Raycast(ray, out hit))
            {
                if (hit.collider != null && hit.collider.gameObject == this.gameObject)
                {
                    Debug.Log("Mouse is over the image!");
                }
                else
                {
                    Debug.Log("Mouse is not over the image.");
                }
            }
        }
    }
}
 

根据你的具体需求,选择适合的检测方法。如果你的图片在Canvas上,推荐使用第一种或第二种方法;如果你的图片是3D对象,推荐使用第三种方法。

相关内容

热门资讯

裸辞做“一人公司”,我后悔了 去年这个时候,一位以色列程序员正在东南亚旅行。他顺手把一个在脑子里转了很久的想法做成了产品,一个让任...
南京建成国内首个Pre-6G试... 4月21日,2026全球6G技术与产业生态大会在南京开幕。全息互动技术展台前,一名远在北京的工作人员...
超梵求职受邀参加“2025抖音... 超梵求职受邀参加“2025抖音巨量引擎成人教育行业生态大会”,探讨分享优质内容传播,服务万千学员。 ...
摩托罗拉Razr 2026(R... IT之家 4 月 22 日消息,摩托罗拉宣布新一代 Razr 折叠手机将于 4 月 29 日在美国发...
库克卸任,特纳斯领航:苹果新纪... 苹果首席执行官蒂姆·库克将卸任,硬件工程主管约翰·特纳斯将接任,苹果公司今天宣布此事。 库克将在夏季...