上一章我们实现了“Unity3d C# 实现UGUI 输入框调用软键盘输入的完整功能(含工程源码,适用触屏一体机等)”,该方式只适用于Windows平台,目前是win10上测试的,其它系统环境待测试而且极可能是不可用的。就进行内置键盘的开发,但目前仅支持英文输入,后续可能会尝试中文输入,但是目前还有一定的难度和限制。
中文离线输入法已实现
https://blog.csdn.net/qq_33789001/article/details/119329690
实现的方法重点在搭建UI,因为键盘的按键还是较多,我这边稍作了一点整理,主要参照手机的输入法键盘布局方式,没有了实体的F1–F12、Alt、Ctrl、Win等按钮,增加了清空、取消、确认等功能按钮。
搭建的最终效果:
只不过通用的按钮是通过按钮的预制体动态加载生成的,包括键值的设置也是动态进行。
先按每行设定了按钮的值数组:
private string[][] Line0_KeyValue = {
new string[]{"`","1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "-", "="},
new string[]{"·", "!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "_", "+" }};
private string[][] Line1_KeyValue = {
new string[]{"q","w", "e", "r", "t", "y", "u", "i", "o", "p", "[", "]", "\\"},
new string[]{"Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P", "{", "}", "|" }};
private string[][] Line2_KeyValue = {
new string[]{"a","s", "d", "f", "g", "h", "j", "k", "l", ";", "'"},
new string[]{"A", "S", "D", "F", "G", "H", "J", "K", "L", ":", "\""}};
private string[][] Line3_KeyValue = {
new string[]{"z","x", "c", "v", "b", "n", "m", ",", ".", "/"},
new string[]{"Z", "X", "C", "V", "B", "N", "M", "<", ">", "?"}};
每个键的值有两个,shift按钮切换。
根据上面的值初始化按键:
//初始化按钮
private void InitComBtn()
{
InstantLineComBtns(Line0, Line0_KeyValue);
InstantLineComBtns(Line1, Line1_KeyValue);
InstantLineComBtns(Line2, Line2_KeyValue);
InstantLineComBtns(Line3, Line3_KeyValue);
}
//按行实例化按钮
private void InstantLineComBtns(Transform LineTran,string[][] KeyValues)
{
for (int i = 0; i < KeyValues[0].Length; i++)
{
GameObject TempObj = GameObject.Instantiate<GameObject>(ComBtnPref);
TempObj.transform.SetParent(LineTran);
TempObj.transform.localScale = Vector3.one;
ComBtn comBtnCtrl = TempObj.GetComponent<ComBtn>();
if (comBtnCtrl != null)
{
comBtnCtrl.SetKeyValue(KeyValues[0][i], KeyValues[1][i]);
OnShiftOn += comBtnCtrl.OnShiftOn;
OnShiftOff += comBtnCtrl.OnShiftOff;
}
}
}
初始化后设置键值,同时绑定shift的状态切换函数,因为shift切换时,每个按钮也将会进行键值的切换。
调用:
if (Keyboard.Instance)
Keyboard.Instance.ShowKeyboard(KeyboardPara, EditCallBack);
接口:
// 唤起键盘接口
public void ShowKeyboard(KeyboardParam para, EventCommon.CallBack<KeyboardParam> call)
{
KeyboardPara = para;
NewEditeString = KeyboardPara.InputStr;
KeyboardWindow.localScale = Vector3.one;
if (call != null)
this.call = call;
}
KeyboardParam 是键盘的参数类,主要参数就是输入(InputStr)和输出(OutputStr)。
每个按钮都会进行点击事件的绑定,当按钮点击时根据当前shift状态,在输入值追加相关输入值。
按键的事件:
//按键事件
public void KeyClick() {
if (Keyboard.Instance != null) {
if (Keyboard.Instance.isShift)
Keyboard.Instance.AddComBtnString(ShiftKey);
else
Keyboard.Instance.AddComBtnString(Key);
}
}
输入内容追加:
public void AddComBtnString(string str)
{
NewEditeString += str;
if (KeyboardPara != null)
KeyboardPara.OutputStr = NewEditeString;
call?.Invoke(KeyboardPara);
if (isShift && !isShiftLock)
{
isShift = false;
ShiftBG.color = new Color(128, 128, 128, 255);
OnShiftOff();
}
}
## 清除功能
直接将内容清空,但点击确定才会生效:
```csharp
//清除点击事件
private void ClickClear()
{
NewEditeString = "";
if (KeyboardPara != null)
KeyboardPara.OutputStr = NewEditeString;
call?.Invoke(KeyboardPara);
}
private void ClickBackSpace()
{
if (!string.IsNullOrEmpty(NewEditeString))
{
NewEditeString = NewEditeString.Substring(0, NewEditeString.Length - 1);
if (KeyboardPara != null)
KeyboardPara.OutputStr = NewEditeString;
call?.Invoke(KeyboardPara);
}
}
//取消点击事件
private void ClickCancel()
{
NewEditeString = "";
if (KeyboardPara != null)
KeyboardPara.OutputStr = KeyboardPara.InputStr;
call?.Invoke(KeyboardPara);
KeyboardPara = null;
KeyboardWindow.localScale = Vector3.zero;
}
//确认点击事件
private void ClickEnter()
{
if (KeyboardPara != null)
KeyboardPara.OutputStr = NewEditeString;
KeyboardWindow.localScale = Vector3.zero;
call?.Invoke(KeyboardPara);
KeyboardPara = null;
}
确认和取消都会关闭输入界面,不同的是取消后值将变成调起键盘前的值,确认就确认了所有的输入和更改。
https://download.csdn.net/download/qq_33789001/20553989
遗憾的就是中文没实现,后续争取实现,有问题请在评论区留言。