Unity Input 鼠标模拟

来源:互联网 发布:网络用语安利的意思 编辑:程序博客网 时间:2024/05/21 04:21

Unity官方用户手册里有这么一段话:

Mouse Simulation

On top of native touch support Unity iOS/Android provides a mouse simulation. You can use mouse functionality from the standardInput class.

翻译为:

鼠标模拟

除了原有的触摸支持以外,Unity iOS/Android还提供了鼠标模拟。你可以通过标准的Input类来使用鼠标功能。

 

那么,Input类中有哪些鼠标功能呢?

查看Input类的成员可以找到

 

anyKeyIs any key or mouse button currently held down? (Read Only)anyKeyDownReturns true the first frame the user hits any key or mouse button. (Read Only)

 在iOS/Android中,手指按下(anykey)或一直触摸不松开(anykeydown)即可返回true

mousePositionThe current mouse position in pixel coordinates. (Read Only)

 返回手指触摸的屏幕坐标

GetMouseButtonReturns whether the given mouse button is held down.GetMouseButtonDownReturns true during the frame the user pressed the given mouse button.GetMouseButtonUpReturns true during the frame the user releases the given mouse button.

手指按下,松开或一直触摸不松开即可返回true

 

我们在iOS/Android中任一手指触摸都被视为"mouse 0",也就是KeyCode.Mouse0,对应着鼠标左键。

 

实际上,除了上述成员外,input类的其它一些成员也具有鼠标功能:

GetAxisReturns the value of the virtual axis identified by axisName.GetAxisRawReturns the value of the virtual axis identified by axisName with no smoothing filtering applied.GetButtonReturns true while the virtual button identified by buttonName is held down.GetButtonDownReturns true during the frame the user pressed down the virtual button identified by buttonName.GetButtonUpReturns true the first frame the user releases the virtual button identified by buttonName.这五个函数通过Edit->Project Settings->Input中定义的虚拟轴和虚拟按钮来检测输入信息。我们可以定义虚拟轴代表鼠标运动增量(Type选择Mouse Movement,自定义的“Mouse X”,"Mouse Y"就是这样的),也可以定义虚拟按钮代表鼠标按键(Type选择Key or Mouse Button,negative button和positve button写上mouse 0(1,2,3,4...))


GetKeyReturns true while the user holds down the key identified by name. Think auto fire.GetKeyDownReturns true during the frame the user starts pressing down the key identified by name.GetKeyUpReturns true during the frame the user releases the key identified by name.这三个函数通过实际的键值来检测输入。可以使用字符串或KeyCode来直接指定要检测的键。我们当然也可以使用KeyCode.Mouse0(1,2,3,4...)或"mouse 0(1,2,3,4...)"来检测鼠标按键是否按下


所以我们也可以在移动设备中使用上述函数来实现鼠标模拟。


值得注意的是,使用多个手指触摸移动设备时,mousePosition的值为所有触摸位置的中心点。

0 0
原创粉丝点击