Unity3D开发之webgl键盘事件监听和其他js输入冲突问题

在将Unity WebGL 3D程序嵌入大型平台时,可能会遇到其他HTML页面的输入键盘事件被Unity劫持的问题。博客详细介绍了Unity WebGL默认监听所有键盘事件的原因及如何通过修改WebGLInput.captureAllKeyboardInput属性来解决此问题。

我们在做一个大的集控平台的时候,很多时候会把unity导出的webgl 3D程序嵌到大平台页面中,属于大平台的一个子功能。在打开3d程序之后再切换到其他js页面的时候,我们会发现其他页面的输入键盘事件不好使了。比如一个input无法输入,也无法删除input里的文字。即input事件被劫听了。

我们打开unity官方文档搜webgl input就会发现这么一段话:


By default, Unity WebGL will process all keyboard input send to the page, regardless of whether the WebGL canvas has focus or not. This is done so that a user can start playing a keyboard-based game right away without the need to click on the canvas to focus it first. However, this can cause problems when there are other HTML elements on the page which should receive keyboard input, such as text fields - as Unity will consume the input events before the rest of the page can get them. If you need to have other HTML elements receive keyboard input, you can change this behavior using the WebGLInput.captureAllKeyboardInput property.

就是说默认情况下unity的webgl会监听所有的键盘事件,无论你是否聚焦这个canvas,只要你打开程序,我们就监听。目的是为了让玩家更加专注游戏 而不需要去点击聚焦程序。但是如果你还有其他的html页面,并且其他页面也需要监听这个事件,那就需要改变这个默认设置。使用WebGLInput.captureAllKeyboardInput这个属性。所以我们在程序中Awake函数中加入设置这个属性的代码。

WebGLInput.captureAllKeyboardInput=false;

 

评论 17
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值