在Unity中将鼠标向上或向上触摸 [英] Mouse up vs. touch up in Unity
问题描述
我有一个问题.我似乎找不到一种很好的方法来区分Unity是触发鼠标还是触发触摸事件.触摸是指用户在触摸触摸屏上的某物后从屏幕上松开手指.
I have a bit of an issue. I can't seem to find a good way to distinguish whether Unity is firing a mouse up vs. a touch up event. By touch up I mean when the user releases their finger from the screen after touching something on a touch display.
问题是这样的:我在游戏逻辑上遇到了一个问题,即用户都松开了用鼠标选择的东西,同时触摸/释放触摸以单击UI按钮(换句话说,尝试仅检测鼠标释放或仅触摸释放-我该怎么做?).我尝试同时使用Input.GetMouseButtonUp(0)
和Input.GetMouseButton(0)
,但是如果发生触摸或鼠标按下事件,两者都会更改其状态.
The problem is this: I have an issue with game logic where the user both lets go of something they've selected with their mouse and SIMULTANEOUSLY touches / releases their touch to click a UI button (in other words, I'm trying to detect a mouse release only, or, a touch release only - how can I do that?). I've tried using both Input.GetMouseButtonUp(0)
as well as Input.GetMouseButton(0)
, but both change their state if its a touch up or a mouse button up event.
那么您如何正确区分两者?我什至尝试使用Touch.fingerId
属性,该属性很好地跟踪/区分了唯一的触摸事件,但是问题是我仍然无法区分只有鼠标悬停的Input.GetMouseButton(0)
,因为即使在触摸时它也会触发.
So how do you properly distinguish between the two? I even tried using the Touch.fingerId
property which works well to track / distinguish ONLY touch events, but, the problem is then that I still cannot distinguish ONLY mouse up with Input.GetMouseButton(0)
since it fires even in the event of a touch up.
外面的人碰巧知道在Unity中分别检测鼠标释放或触摸释放的正确方法是什么吗?
Does anyone out there happen to know what the proper way would be to detect just a mouse release, or just a touch release, separately, in Unity?
修改:
有些人不了解当前的问题,因此让我们假设您有一台支持触摸的台式机设备.添加此脚本:
Someone didn't understand the problem at hand, so let's assume for a second you have a desktop device with touch support. Add this script:
void Update () {
if (Input.GetMouseButtonDown(0))
Debug.Log("Mouse button down.");
if (Input.GetMouseButtonUp(0))
Debug.Log("Mouse button up.");
}
如果在按住的同时单击并按住鼠标,则可以用手指触摸并释放,这会记录Mouse button up.
,尽管您没有释放鼠标键.如何区分鼠标向上和触摸释放?
If you click and HOLD with your mouse, while holding, you can touch and release with your finger, which will log Mouse button up.
, despite the fact that you did not release the mouse button. How can I distinguish between a mouse up vs. a touch release?
推荐答案
我知道了.
我错过了这个电话:Input.simulateMouseWithTouches = false;
如果仅在Start()事件上设置它似乎可行(我可能是错的,可能是您需要在更新的第一个Input
访问器之前进行设置,对其进行测试我猜出来).否则,Unity会根据触摸事件模拟鼠标事件.我只能推测他们这样做是为了使游戏开发人员更容易编写一次并跨平台玩游戏.
Seems to work if you set this on a Start() event only (I could be wrong, it may be you need to set it before the first Input
accessor on update, test it out I guess). Otherwise, Unity simulates mouse events from touch events. I can only speculate they do this in order to make it easier for game developers to write once and play cross-platform.
注意:设置完毕后,将其设置为全局设置,因此即使在以后加载的其他场景中也需要取消设置.
Note: Once its set, its set globally, so you’ll need to unset it even in a different scene you load later on.
这篇关于在Unity中将鼠标向上或向上触摸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!