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类的成员可以找到
anyKey | Is any key or mouse button currently held down? (Read Only) |
---|---|
anyKeyDown | Returns true the first frame the user hits any key or mouse button. (Read Only) |
在iOS/Android中,手指按下(anykey)或一直触摸不松开(anykeydown)即可返回true
mousePosition | The current mouse position in pixel coordinates. (Read Only) |
---|
返回手指触摸的屏幕坐标
GetMouseButton | Returns whether the given mouse button is held down. |
---|---|
GetMouseButtonDown | Returns true during the frame the user pressed the given mouse button. |
GetMouseButtonUp | Returns true during the frame the user releases the given mouse button. |
手指按下,松开或一直触摸不松开即可返回true
我们在iOS/Android中任一手指触摸都被视为"mouse 0",也就是KeyCode.Mouse0,对应着鼠标左键。
实际上,除了上述成员外,input类的其它一些成员也具有鼠标功能:
GetAxis | Returns the value of the virtual axis identified by axisName. |
---|---|
GetAxisRaw | Returns the value of the virtual axis identified by axisName with no smoothing filtering applied. |
GetButton | Returns true while the virtual button identified by buttonName is held down. |
GetButtonDown | Returns true during the frame the user pressed down the virtual button identified by buttonName. |
GetButtonUp | Returns true the first frame the user releases the virtual button identified by buttonName. |
GetKey | Returns true while the user holds down the key identified by name. Think auto fire. |
---|---|
GetKeyDown | Returns true during the frame the user starts pressing down the key identified by name. |
GetKeyUp | Returns true during the frame the user releases the key identified by name. |
所以我们也可以在移动设备中使用上述函数来实现鼠标模拟。
值得注意的是,使用多个手指触摸移动设备时,mousePosition的值为所有触摸位置的中心点。