大家好,我读到 Unity 并不真正支持 WebGL 游戏的移动浏览器。我使用的是 2020.1.4。果然,游戏因未正确缩放而有点扭曲。就像相机更大一样,所以它在屏幕上显示蓝色。我尝试了一些方法,将宽度和高度设置为自动或删除 config.devicePixelRatio = 1;按照朋友的建议,但不!看起来还是很可怕!如果这还不够,单击表单字段时键盘不会显示。我试过这个 https://github.com/eforerog/keyboardMobileWebGLUnity 按下时显示错误,而这个 https://github.com/dantasulisses/WebMobileInputFix 甚至没有编译!
请问有什么想法吗?
我做了研究并尝试了我能找到的所有插件。我使用 Unity 2020.3.28f1 并在 Android 手机和 iPhone 上进行了测试。这是我的报告。
这些插件不起作用:
https://unitylist.com/p/f58/Unity-webgl-inputfield
https://github.com/eforerog/keyboardMobileWebGLUnity
https://github.com/dantasulisses/WebMobileInputFix
此插件可以工作,但您应该在同一输入字段游戏对象上对 IOS 和 Android 使用不同的设置。如果您使用“prompt”,则仅适用于 IOS,“overlay”仅适用于 Android。在页面中查找文档:
https://github.com/unity3d-jp/WebGLNativeInputField
这个插件目前效果最好。是的,虽然有点难看,但它确实有效。
https://github.com/kou-yeung/WebGLInput
Unity 2021 有一个修复程序:
https://github.com/kou-yeung/WebGLInput/releases/tag/1.0
有一个覆盖键盘,使用时只需点击通知即可访问它,然后单击“后退”按钮将其隐藏https://play.google.com/store/apps/details?id= com.fishstix.gameboard
我制作了这个项目,它只是使用统一的按钮重新创建键盘。 我在 WebGL 构建中成功实现了它。
https://github.com/thetimeste/WebGL-Build-Keyboard-Unity.git
我建议在撰写本文时使用原生 js window.prompt() 字段。它们具有出色的跨平台支持,允许使用特殊字符、表情符号、复制和粘贴等额外功能,并且非常容易设置。一旦(或者说实话,如果有的话)Unity 添加了自己的可靠实现,您就可以轻松删除这个轻量级实现。
就是这样。结果应该类似于此演示:https://pop.demo.neoludic.games
如果你想节省一些开发时间来开发一个真正应该在Unity中原生的功能,你也可以根据上面的方法查看我的插件。 https://neoludic-games.itch.io/pop-input
我能够通过使用额外的脚本解决这个问题。
index.html
<!-- Keyboard Input System -->
<input id="keyboardInput" type="text" style="position:fixed; z-index: -100;">
<script>
const keyboardInput = document.getElementById('keyboardInput');
const unityCanvas = document.getElementById('unity-canvas');
window.showKeyboard = false;
let firstClick = false;
unityCanvas.addEventListener('click', async function acceptFirstClick() {
if(firstClick) return;
firstClick = true;
while(true)
{
if(window.showKeyboard)
{
window.showKeyboard = false;
keyboardInput.focus(); // calling mobile keyboard
}
await new Promise(r=>setTimeout(r,400));
}
});
document.body.addEventListener('input', e => {
if (e.inputType == "deleteContentBackward" || (e.inputType == 'insertCompositionText' && e.data == null)) {
window.unityInstance.SendMessage('InputReceiver', 'Backspace');
}
else if (e.data == null) {
return;
}
else if (e.data.length > 0) {
window.unityInstance.SendMessage('InputReceiver', 'AcceptInput', e.data[e.data.length - 1]);
}
});
</script>
external.jslib
// ...
ShowMobileKeyboard: function() {
window.showKeyboard = true;
},
// ...
外部输入接收器.cs
public class ExternalInputReceiver : MonoBehaviour
{
TMPro.TMP_InputField GetCurrentInputField()
{
EventSystem system = EventSystem.current;
GameObject currentobj = system.currentSelectedGameObject;
if (currentobj == null) return null;
var inputField = currentobj.GetComponent<TMPro.TMP_InputField>();
return inputField;
}
public void AcceptInput(string textInput)
{
var inputField = GetCurrentInputField();
if (inputField != null)
inputField.text += textInput;
}
public void Backspace()
{
var inputField = GetCurrentInputField();
if (inputField != null)
if (inputField.text.Length > 0)
inputField.text = "";
}
public void InvokeMobileKeyboard() {
if (GetCurrentInputField() == null) return;
ShowMobileKeyboard();
}
[DllImport("__Internal")]
private static extern void ShowMobileKeyboard();
}
重要:要在 Unity 元素接收焦点时调用键盘,需要为每个“InputField”组件添加一个“ExternalInputReceiver.InvokeMobileKeyboard”处理程序到编辑器中的“onSelect”事件。
缺点:“退格键”会删除所有用户输入。我这样做是为了跨平台,因为某些设备在退格输入事件中从先前的输入生成数据。
解释:解决方案是创建一个不可见字段,当内部输入捕获焦点时,该字段捕获焦点。由于在用户操作的情况下调用元素上的“focus()”函数,因此我们向处理程序添加异步,以便能够随时调用移动键盘。 我在多个设备上进行了测试。它在桌面上也可以正常工作。改进是可能的。
我还需要启用移动虚拟键盘才能在移动设备上运行 webgl。 我已经尝试过您提到的网址中的代码。它为您提供了一些如何做的想法 它,但代码完全有错误且无法使用。现在我正在尝试实施它 我自己。