Unity Input 鼠标模拟

Unity iOS/Android 提供了鼠标模拟功能,允许开发者在移动平台上通过Input类使用鼠标操作。当手指触摸屏幕时,Input.anyKey或Input.anyKeyDown返回true,Input.mousePosition表示触摸点的屏幕坐标。每个触摸被视为"mouse 0",对应鼠标左键。此外,Input类的其他成员也可用于鼠标功能的模拟。在多指触摸时,mousePosition是所有触摸点的中心位置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.
这五个函数通过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...))


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.
这三个函数通过实际的键值来检测输入。可以使用字符串或KeyCode来直接指定要检测的键。我们当然也可以使用KeyCode.Mouse0(1,2,3,4...)或"mouse 0(1,2,3,4...)"来检测鼠标按键是否按下


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


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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值