实现原理是使用JS调用浏览器的默认提示框,默认提示框可以呼出软键盘,(注意此时呼出的提示框会阻塞,unity会不工作,就算你勾选了Run in BackGround),关掉提示框,unity会再次工作
网上给的解决方法是自定义呼出模板,而不是用默认提示框(博主没试过)
1,创建JS代码(放在Plugin中,这方面Unity官网有介绍如何制作unity可调用的JS的操作方法),Unity调用JS
JS代码
mergeInto(LibraryManager.library, {
StringReturnValueFunction: function ()
{
var returnStr = window.location.search;
var buffer = _malloc(lengthBytesUTF8(returnStr) + 1);
writeStringToMemory(returnStr,buffer);
return buffer;
},
ShowKeyboard: function (str) //这个就是呼出浏览器默认提示框的方法
{
var returnStr = prompt("", Pointer_stringify(str)); //呼出提示框方法,具体百度
if(returnStr == undefined)
{
return "";
}
var buffer = _malloc(lengthBytesUTF8(returnStr) + 1);
writeStringToMemory(returnStr,buffer);
return buffer;
},
});
Unity调用
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System.Runtime.InteropServices;
using System;
[RequireComponent(typeof(InputField))]
public class KeyboardClassInput : MonoBehaviour, ISelectHandler
{
[DllImport("__Internal")]
private static extern string ShowKeyboard(string str);
string str;
private InputField inputField;
private void Awake()
{
inputField = GetComponent<InputField>();
}
public void Update()
{
if (str != null&&str!=""&&!string.IsNullOrEmpty(str))
{
inputField.text= str;
}
}
public void OnSelect(BaseEventData data)
{
#if UNITY_EDITOR
return;
#endif
str = ShowKeyboard("Enter Name");
}