当前位置: 首页 > news >正文

unity里鼠标位置是否在物体上。

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<RaycastResult> results = new List<RaycastResult>();
        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<RectTransform>();
        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对象,推荐使用第三种方法。

http://www.lryc.cn/news/390837.html

相关文章:

  • Java知识点大纲
  • 【Kafka】记录一次Kafka消费者重复消费问题
  • Android使用http加载自建服务器静态网页
  • python解耦重构,提高程序维护性
  • 深入解析 Laravel 事件系统:架构、实现与应用
  • 视频怎么制作gif动态图片?GIF制作方法分享
  • js 使用 lodash-es 检测某个值是否是函数
  • [go-zero] goctl 生成api和rpc
  • docker -run hello-world超时
  • 拓扑学习系列(8)黎曼度量曲面与高斯曲率
  • 汽车IVI中控开发入门及进阶(三十四):i.MX linux BSP
  • 【Python机器学习】算法链与管道——构建管道
  • Postman 高级用法学习
  • 从新手到高手:Scala函数式编程完全指南,Scala 访问修饰符(6)
  • IDEA 一键部署Docker
  • linux centos tomcat 不安全的HTTP请求方法
  • 深入Django(六)
  • PHP房产小程序微信小程序系统源码
  • vscode 历史便携版下载方法
  • 深入理解计算机系统 CSAPP 家庭作业8.19
  • JDBC学习(Java DataBase Connectivity)
  • 2024源代码加密软件评测丨保护企业源代码防泄密
  • 测试图片上传功能,使用postman提供的url
  • 使用dot来画流程图
  • 基于Java的微信记账小程序【附源码】
  • uniapp中 uni.previewImage用法
  • 第十三章 StringTable
  • Adobe Acrobat添加时间戳服务器
  • 数据库管理-第217期 Oracle的高可用-02(20240704)
  • 搭建基础库~