检测鼠标单击 GUI [英] Detect mouse clicked on GUI

查看:49
本文介绍了检测鼠标单击 GUI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的项目有问题.我想知道鼠标 cliked 发生在 GUI 或任何游戏对象上.我试过了,但它显示空引用异常

I got a problem in my project. I want to know that mouse cliked happend on GUI or on any game object. I have tried this but it is showing null reference exception

EventSystem eventSystem = EventSystem.current;
            if (eventSystem.IsPointerOverGameObject())
               Debug.Log("left click over a gui element");

如何检测??有没有可用的活动?

how to detect?? Is there any event available or else?

推荐答案

终于得到我的答案 此处:有三种方法可以做到这一点,如本视频教程所示.这个视频救了我:)

Finally got my answer here: There are three ways to do this, as demonstrated in this video tutorial. this video save me:).

  1. 使用 EventSystem.current.IsPointerOverGameObject

  1. Use EventSystem.current.IsPointerOverGameObject

将您的 OnMouseXXX 和 Raycasts 转换为 EventSystem 触发器.在相机上使用物理光线投射器

Convert your OnMouseXXX and Raycasts to an EventSystem trigger. Use a physics raycaster on the camera

从 EventSystems 命名空间实现各种处理程序接口.在相机上使用物理光线投射器.

Implement the various handler interfaces from the EventSystems namespace. Use a physics raycaster on the camera.

这篇关于检测鼠标单击 GUI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆