【unity】自定义键盘插件

首先需要实现一个自定义的input组件--MyInputField,因为unity默认的inputfield组件默认都是唤起系统键盘,所以需要重新写一个组件,继承自Input Field和IPointerClickHandler

public class MyInputField : InputField, IPointerClickHandler
{
    public class ClickEvent : UnityEvent {}
    private ClickEvent _onClickEvent = new ClickEvent();

    public ClickEvent OnFocused
    {
        get { return _onClickEvent; }
        set { _onClickEvent = value; }
    }

    
    public  void OnPointerClick(PointerEventData eventData)
    {
        if (isFocused)
        {
            print("isFocused");
            _onClickEvent?.Invoke();
        }
        
    }
   
}

使用自定义的MyInputField组件去唤起自定义键盘

public MyInputField myinput;//自定义输入
private static KeyboardParam KeyboardPara = new KeyboardParam("");  //键盘参数
public GameObject keyboardObj;//键盘预制体实例化的对象

void Awake()
{
    if(myinput==null) myinput=transform.GetOrAddComnent();
}

void inputAction()
{
        myinput.text = "";
        myinput.placeholder.GetComponent().text = Placeholder;
        myinput.OnFocused.RemoveAllListeners();
        myinput.OnFocused.AddListener(() =>
        {
            ClickText();
        });
        void ClickText()
        {
            KeyboardPara.InputStr = input.text;
            CnKeyboard cnKeyboard = keyboardObj.GetComponentInChildren();

            if (cnKeyboard!=null)
                cnKeyboard.ShowKeyboard(KeyboardPara, EditCallBack);
        //EditCallBack回调,返回输入的值kbpara.OutputStr赋值给自定义输入框
        void EditCallBack(KeyboardParam kbpara)
          {
            myinput.text = kbpara.OutputStr;
          }
        }     

}

cnKeyboard脚本的初始化时间比较长,需要等待其Awake和Start执行完之后执行ShowKeyboard才不会出错,可以使用UniTask封装一下,将Awake和Start里面的逻辑封装一下,等这些操作完成之后再执行ShowKeyboard,以免操作过快未初始化完就调用导致的error。

using UnityEngine;
using hyjiacan.py4n;
using UnityEngine.UI;
using System.Collections.Generic;
using System.Text.RegularExpressions;

namespace InnerKeyboard
{
    //键盘参数类
    public class KeyboardParam
    {
        public string InputStr;
        public string OutputStr;

        public KeyboardParam(string InStr, string OutStr = "")
        {
            InputStr = InStr;
            OutputStr = OutStr;
        }
    }

    //委托事件类
    public class EventCommon
    {

        public delegate void CallBack(T para);

        public delegate void NorEvent();
    }

    public class CnKeyboard : MonoBehaviour
    {
        private RectTransform KeyboardWindow;
        private GameObject ComBtnPref;
        private Transform Line0, Line1, Line2, Line3;
        private Button BackSpaceBtn, ShiftBtn, SpaceBtn, CancelBtn, EnterBtn, LangugeBtn, ClearBtn;
        private Image ShiftBG;

        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", "<", ">", "?"}};


        private string NewEditeString = "";

        [HideInInspector]
        public bool isShift = false;
        private bool isShiftLock = false;
        private float ShiftTime = 0f;
        private Color32 LockColor = new Color32(127, 171, 179, 255);


        private event EventCommon.NorEvent OnShiftOn = null;
        private event EventCommon.NorEvent OnShiftOff = null;

        private KeyboardParam KeyboardPara = null;//键盘参数


        private EventCommon.CallBack call = null; //回调函数

        private static CnKeyboard instance = null;

        bool isCn = false;
        Text LanText, PinYinText;
        string PinYinStr;   //拼音串
        string[] HanZiArr = new string[] { };  //汉子的结果值;
        List HanZiTextList = new List();

        int NowPage = 0, TotalPage = 0, PageSize = 10;
        Button LastBtn, NextBtn;
        GameObject CnObj;

        public static CnKeyboard Instance
        {
            get { return instance; }
        }

        //初始化部分节点
        private void Awake()
        {
            KeyboardWindow = this.transform.GetComponent();
            ComBtnPref = Resources.Load("KeyItem");
            Line0 = KeyboardWindow.Find("KB_BG/KeyBtns/ComBtnLine0");
            Line1 = KeyboardWindow.Find("KB_BG/KeyBtns/ComBtnLine1");
            Line2 = KeyboardWindow.Find("KB_BG/KeyBtns/ComBtnLine2");
            Line3 = KeyboardWindow.Find("KB_BG/KeyBtns/ComBtnLine3");

            BackSpaceBtn = KeyboardWindow.Find("KB_BG/KeyBtns/BtnLine3/BackSpaceBtn").GetComponent

源码:后续资源上传后评论区添加

你可能感兴趣的:(unity,java,游戏引擎)