一、开局先上个小键盘效果图(UI为NGUI所制作,这里不再赘述)
二、代码部分(代码可扩展部分非常强)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
public class SmallKeyboard : MonoBehaviour
{
public enum KeyboardType
{
Number = 0,
Character = 1,
EnterKey = 2,
BackSpace = 3
}
[Serializable]
public class CUIKeyInfo
{
public GameObject KeyObj;
public KeyboardType KeyType;
public string SValue;
}
//输入的值
public string strInput = "";
public List<CUIKeyInfo> list = new List<CUIKeyInfo>();
string sReslut = "";
private void ProcessKeyEvent(CUIKeyInfo _info)
{
if (_info.KeyType == KeyboardType.Character || _info.KeyType == KeyboardType.Number)
{
//这里注意光标,如果光标移动到不是最后一个元素的某个位置,要考虑插入的方法
//sReslut = sReslut.Insert(strTarget.IndexOf(cursorIndex), _info.SValue);
sReslut += _info.SValue;
}
else if (_info.KeyType == KeyboardType.BackSpace)
{
//这里注意光标,如果光标移动到不是最后一个元素的某个位置,要考虑中间删除的方法
//sReslut = sReslut.Remove(cursorIndex -1,cursorIndex);
sReslut = sReslut.Remove(sReslut.Length - 1, sReslut.Length);
}
else if (_info.KeyType == KeyboardType.EnterKey)
{
Debug.LogError("sResult = " + sReslut);
}
}
private void ClickButton(GameObject go)
{
for (int i = 0; i < list.Count; i++)
{
if (list[i].KeyObj == go)
{
ProcessKeyEvent(list[i]);
break;
}
}
}
// Use this for initialization
void Start()
{
for (int i = 0; i < list.Count; i++)
{
CUIKeyInfo info = list[i];
UIEventListener.Get(info.KeyObj).onClick = ClickButton;
}
}
}
三、挂载脚本部分
注意:本资源为原创,转载请注明出处。请勿用作商业用途