美文网首页
【Unity实现单机功能】自定义按键输入

【Unity实现单机功能】自定义按键输入

作者: 黎明之钥 | 来源:发表于2019-09-24 20:54 被阅读0次

实现自定义多个数的按键输入
将CustomKey.cs挂在空物体上,并将Button对象拖入预留位置

//CustomKey.cs 
using UnityEngine;
using System.Collections.Generic;
using UnityEngine.UI;

/// <summary>
/// 自定义按键
/// </summary>
public class CustomKey : MonoBehaviour
{
    //声明Button对象以进行拖拽
    public Button resetBtn;
    public Button jumpBtn;
    public Button getDownBtn;
    public Button leftHeadBtn;
    public Button rightHeadBtn;
    public Button musicSwitchBtn;

    //键位数量限制
    public int keyCountLimit = 2;

    public CustomButton jump;
    public CustomButton getDown;
    public CustomButton leftHead;
    public CustomButton rightHead;
    public CustomButton musicSwitch;

    void Awake()
    {
        resetBtn.onClick.AddListener(CustomButton.ResetBindKeys);
        resetBtn.transform.FindChild("Text").GetComponent<Text>().text = "Reset";                
        jump = new CustomButton("Jump", jumpBtn, new KeyCode[]{ KeyCode.Space}, keyCountLimit);
        getDown = new CustomButton("GetDown", getDownBtn, new KeyCode[] { KeyCode.LeftControl}, keyCountLimit);
        leftHead = new CustomButton("LeftHead", leftHeadBtn, new KeyCode[] { KeyCode.Q}, keyCountLimit);
        rightHead = new CustomButton("RightHead", rightHeadBtn, new KeyCode[] { KeyCode.E}, keyCountLimit);
        musicSwitch = new CustomButton("musicSwitch", musicSwitchBtn, new KeyCode[] { KeyCode.LeftControl, KeyCode.M}, keyCountLimit);     
    }

    void Update()
    {
        if (CustomButton.isPlaying)
        {
            jump.Function();
            getDown.Function();
            leftHead.Function();
            rightHead.Function();
            musicSwitch.Function();
        }
    }

    void OnGUI()
    {
        if (CustomButton.isWaitingForKey)
        {
            Event e = Event.current;
            if (e.isKey && e.keyCode != KeyCode.None)
            {                
                if (!CustomButton.tempKeyList.Contains(e.keyCode)) //如果list中不包含该按键,则记录按键
                {
                    CustomButton.tempKeyList.Add(e.keyCode);
                    CustomButton.tempPressList.Add(false);                    
                    CustomButton.currentButton.buttonText.text = CustomButton.MergeText(CustomButton.tempKeyList, keyCountLimit);
                }      
            }
            if (CustomButton.tempKeyList.Count > 0) //如果已经开始记录新按键
            {
                bool result = true;
                for (int i = 0; i < CustomButton.tempKeyList.Count ; i++)
                {
                    if(Input.GetKey(CustomButton.tempKeyList[i])) 
                        CustomButton.tempPressList[i] = false;                                   
                    else
                        CustomButton.tempPressList[i] = true;
                }
                for (int i = 0; i < CustomButton.tempKeyList.Count; i++)
                {
                    result = result & CustomButton.tempPressList[i];
                }
                if (result) //所有已经按下的按键抬起,停止记录按键
                {
                    CustomButton.SetNewKey(keyCountLimit);
                }
            }
        }
    }
}

/// <summary>
/// 将屏幕上的Button与实际键盘按钮、名称绑定
/// </summary>
public class CustomButton
{
    string FunctionName { set; get; } //功能名称
    public Button BindButton { private set; get; } //屏幕上的Button对象
    KeyCode[] DefaultKey; //默认绑定的键位

    int keyCount; //键位数量
    public List<KeyCode> keyList; //当前绑定的键位    
    List<bool> pressList; //记录每个键位的状态
    public Text buttonText; //Button上显示的内容

    public static bool isWaitingForKey = false; //等待键盘输入状态
    public static bool isPlaying = false; //游戏状态

    public static CustomButton currentButton; //当前指向的CustomButton
    public static List<KeyCode> tempKeyList = new List<KeyCode>(); //临时存储的键位
    public static List<bool> tempPressList = new List<bool>(); //临时键位的状态
    static List<CustomButton> buttonList = new List<CustomButton>(); //包含所有CustomButton对象的list

    public CustomButton(string functionName, Button button, KeyCode[] keycode, int limit)
    {
        FunctionName = functionName;
        BindButton = button;
        if (keycode.Length <= limit) //如果键位数量超过限制,只保存前几个键位
        {
            DefaultKey = keycode;
        }
        else
        {
            DefaultKey = new KeyCode[limit];
            for (int i = 0; i < limit; i++)
            {
                DefaultKey[i] = keycode[i];
            }
        }
        
        keyCount = PlayerPrefs.GetInt(FunctionName + "keyCount", DefaultKey.Length); //如果有设定过新按钮,则在 PlayerPrefs中读取按键数量,否则设为默认键位的数量
        keyList = new List<KeyCode>();
        pressList = new List<bool>();

        for (int i = 0; i < keyCount; i++)
        {
            keyList.Add((KeyCode)System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString(FunctionName + i.ToString(), (i < DefaultKey.Length ? DefaultKey[i].ToString() : null)))); //读取按键
            pressList.Add(false);
        }            
            
        buttonText = BindButton.transform.Find("Text").GetComponent<Text>();
        buttonText.text = MergeText(keyList); //在Button上显示当前绑定的键位
        
        BindButton.onClick.RemoveAllListeners(); //Button对象移除所有监听方法      
        BindButton.onClick.AddListener(BtnClick); //Button对象绑定监听方法BtnClick

        buttonList.Add(this); //将自己加入list
    }

    public void BtnClick()
    {
        if (currentButton != this) //如果当前指向的按钮不是自己
        {
            if (currentButton != null) //当前指向的按钮不为空,恢复在之前Button上显示绑定的键位            
                currentButton.buttonText.text = MergeText(currentButton.keyList);
            currentButton = this; //将当前指向的按钮指向自己
        }
        buttonText.text = ""; //将Button上显示的内容设置为""
        tempKeyList.Clear();
        tempPressList.Clear();
        isWaitingForKey = true; //等待键盘输入开启
    }

    /// <summary>
    /// 设置新按键
    /// </summary>
    /// <param name="limit"></param>
    public static void SetNewKey(int limit)
    {
        if (tempKeyList.Count > limit) //如果键位数量超过限制,只保存前几个和最后一个键位
        {
            tempKeyList[limit - 1] = tempKeyList[tempKeyList.Count - 1];
            tempKeyList.RemoveRange(limit, tempKeyList.Count - limit);
        }
        KeyConflictDetection();
        currentButton.keyList.Clear();
        currentButton.pressList.Clear();
        for (int i = 0; i < tempKeyList.Count; i++)
        {
            currentButton.keyList.Add(tempKeyList[i]); //将当前指向的按钮绑定的键位设为传入的键位
            currentButton.pressList.Add(false);
            PlayerPrefs.SetString(currentButton.FunctionName + i.ToString(), currentButton.keyList[i].ToString()); //将设置的键位存入PlayerPrefs,这样可以在重新运行程序后进行读取
        }        
        currentButton.keyCount = currentButton.keyList.Count;
        PlayerPrefs.SetInt(currentButton.FunctionName + "keyCount", currentButton.keyCount);
        currentButton.buttonText.text = MergeText(currentButton.keyList); //在当前指向的按钮的Button上显示当前绑定的键位 
        isWaitingForKey = false; //等待键盘输入关闭
    }

    /// <summary>
    /// 键位冲突检测
    /// </summary>
    static void KeyConflictDetection()
    {
        bool repeated = false;
        foreach (CustomButton button in buttonList)
        {
            if (button != currentButton && button.keyList.Count == tempKeyList.Count) //如果两个CustomButton对象键位长度一致
            {
                for (int i = 0; i < tempKeyList.Count; i++)
                {
                    if (!button.keyList.Contains(tempKeyList[i]))
                    {
                        repeated = false;
                        break;
                    }
                    repeated = true;                     
                }
                if (repeated)
                {
                    button.keyList.Clear();
                    button.pressList.Clear();
                    button.buttonText.text = ""; //将Button显示的内容设为""  
                }                                  
            }
        }
    }

    /// <summary>
    /// 恢复默认键
    /// </summary>
    public static void ResetBindKeys()
    {        
        PlayerPrefs.DeleteAll();
        foreach (CustomButton button in buttonList)
        {
            button.keyList.Clear();
            button.pressList.Clear();
            button.keyCount = button.DefaultKey.Length;
            PlayerPrefs.SetInt(button.FunctionName + "keyCount", button.keyCount);
            for (int i = 0; i < button.keyCount; i++)
            {
                if (!button.keyList.Contains(button.DefaultKey[i])) //如果list中不包含该按键,则加入list
                {
                    button.keyList.Add(button.DefaultKey[i]);
                    button.pressList.Add(false);
                    PlayerPrefs.SetString(button.FunctionName + i.ToString(), button.DefaultKey[i].ToString());                    
                }
            }
            button.buttonText.text = MergeText(button.keyList);
        }
        isWaitingForKey = false; //等待键盘输入关闭      
    }

    /// <summary>
    /// 将按键内容拼接
    /// </summary>
    /// <param name="e"></param>
    /// <returns></returns>
    public static string MergeText(List<KeyCode> e)
    {
        string text = null;
        for (int i = 0; i < e.Count; i++)
        {
            text += e[i].ToString();
            if (e.Count - i > 1)
                text += "+";
        }
        return text;
    }

    public static string MergeText(List<KeyCode> e, int limit)
    {
        if (e.Count <= limit)
            return MergeText(e);
        else
        {
            List<KeyCode> temp = new List<KeyCode>();
            for (int i = 0; i < limit - 1; i++)
            {
                temp.Add(e[i]);
            }
            temp.Add(e[e.Count-1]);
            return MergeText(temp);
        }
    }


    bool functionState = false;
    public void Function()
    {
        bool result = true;

        for (int i = 0; i < keyList.Count; i++)
        {
            if (Input.GetKey(keyList[i]))
            {
                pressList[i] = true;
            }
            else
            {
                pressList[i] = false;
                functionState = false;
            }
        }

        for (int i = 0; i < keyList.Count; i++)
        {
            result = result & pressList[i];
        }

        if (result && !functionState)
        {
            Debug.Log(FunctionName);
            functionState = true;
        }            
    }
}

UISwitch.cs挂在canvas对象上,canvas隐藏后可以检测按键设置是否有效

//UISwitch.cs
using UnityEngine;

public class UISwitch : MonoBehaviour {

    void OnEnable()
    {
        CustomButton.isPlaying = false;
    }

    void OnDisable()
    {
        CustomButton.isPlaying = true;
        if (CustomButton.isWaitingForKey) //如果正在设置新按键时关闭UI,则恢复原先的按键
        {
            CustomButton.currentButton.buttonText.text = CustomButton.MergeText(CustomButton.currentButton.keyList);
            CustomButton.isWaitingForKey = false;
        }
    }
}

相关文章

  • 【Unity实现单机功能】自定义按键输入

    目前只实现了自定义单个按键输入

  • 【Unity实现单机功能】自定义按键输入

    实现自定义多个数的按键输入将CustomKey.cs挂在空物体上,并将Button对象拖入预留位置 UISwitc...

  • 命令模式(1)

    实现玩家自定义按键------基于Unity3D 本文是根据Robert Nystrom所著的Game progr...

  • Android GPIO 控制方案

    此方案实现Android APP控制CPU GPIO,支持设置IO方向、输出、输入、按键功能。 移植驱动与Fram...

  • 转:Unity延时功能的几种实现

    转自《Unity3D项目实战笔记(5):延时功能的几种实现》我所做过的系统,分单机版系统(2005年)、CS系统(...

  • Unity3D项目实战笔记(5):延时功能的几种实现

    转自《Unity3D项目实战笔记(5):延时功能的几种实现》我所做过的系统,分单机版系统(2005年)、CS系统(...

  • 1.3  嵌入式

    今天做了用矩阵键盘按键实现不同的功能,先搭建框架,确定引脚的输入输出,来确定寄存器相应配置,再配置要实现的功能,主...

  • iOS自定义键盘

    对于有输入功能的控件,例如UITextField,可以给控件的inputView属性赋值,实现自定义键盘的功能 点...

  • 一、todolist功能开发

    todolist功能开发todolist,简单的说是由一个框,按键组成。在框内输入内容,点击按键,输入的内容会以一...

  • Pygame贪吃蛇

    点击开始游戏开始 按键盘ESC退出游戏 根据方向按键实现蛇的移动 按空格键实现暂停或者开始功能 实时显示分数功能 ...

网友评论

      本文标题:【Unity实现单机功能】自定义按键输入

      本文链接:https://www.haomeiwen.com/subject/djvbuctx.html