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

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对象,推荐使用第三种方法。

相关内容

热门资讯

科普!创建金花微信链接房卡,微... 微信游戏中心:牛牛房卡,添加微信【55051770】,进入游戏中心或相关小程序,搜索“微信牛牛房卡”...
分享!微信牛牛房卡如何购买/宝... 牛牛是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:33699510许多玩家在游戏中会购买房卡来享受...
科普!如何购买炸金花链接房卡,... 微信游戏中心:炸金花房卡,添加微信【33903369】,进入游戏中心或相关小程序,搜索“微信炸金花房...
科普!想找个炸金花在哪买房卡,... 微信游戏中心:炸金花房卡,添加微信【8488009】,进入游戏中心或相关小程序,搜索“微信炸金花房卡...
科普!玩金花房卡从哪买的,微信... 微信游戏中心:拼三张房卡,添加微信【55051770】,进入游戏中心或相关小程序,搜索“微信拼三张房...
分享!微信斗牛购买房卡/新大海... 斗牛是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:33699510许多玩家在游戏中会购买房卡来享受...
科普!微信开炸金花房卡怎么买,... 微信游戏中心:炸金花房卡,添加微信【33903369】,进入游戏中心或相关小程序,搜索“微信炸金花房...
科普!金花从哪里购买房卡,微信... 微信游戏中心:拼三张房卡,添加微信【8488009】,进入游戏中心或相关小程序,搜索“微信拼三张房卡...
分享!拼三张房卡链接去哪里买/... 拼三张是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:33699510许多玩家在游戏中会购买房卡来享...
科普!微信上炸金花房间买房卡,... 微信游戏中心:炸金花房卡,添加微信【55051770】,进入游戏中心或相关小程序,搜索“微信炸金花房...
科普!微信好友炸金花房卡如何购... 微信游戏中心:拼三张房卡,添加微信【33903369】,进入游戏中心或相关小程序,搜索“微信拼三张房...
分享!炸金花房卡怎么弄/微信金... 炸金花是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:33699510许多玩家在游戏中会购买房卡来享...
科普!金花房卡在哪里买,牛牛房... 微信游戏中心:牛牛房卡,添加微信【8488009】,进入游戏中心或相关小程序,搜索“微信牛牛房卡”,...
分享!玩拼三张房卡从哪里买/新... 拼三张是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:33699510许多玩家在游戏中会购买房卡来享...
科普!微信群打金花房卡怎么搞,... 微信游戏中心:牌九房卡,添加微信【33903369】,进入游戏中心或相关小程序,搜索“微信牌九房卡”...
科普!金花房卡找谁买,微信链接... 微信游戏中心:牛牛房卡,添加微信【8488009】,进入游戏中心或相关小程序,搜索“微信牛牛房卡”,...
分享!微信群链接炸金花房卡/招... 炸金花是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:33699510许多玩家在游戏中会购买房卡来享...
科普!金花房卡怎么购买,微信拼... 微信游戏中心:拼三张房卡,添加微信【55051770】,进入游戏中心或相关小程序,搜索“微信拼三张房...
科普!在哪里买炸金花微信房卡,... 微信游戏中心:炸金花房卡,添加微信【33903369】,进入游戏中心或相关小程序,搜索“微信炸金花房...
科普!金花链接房卡去哪里买,微... 微信游戏中心:牛牛房卡,添加微信【8488009】,进入游戏中心或相关小程序,搜索“微信牛牛房卡”,...