【unity學習筆記】unity實現可配置按鍵輸入

開始

一般情況下,我們這樣來處理遊戲中的輸入:

void Update () {
            if (Input.GetKeyDown(KeyCode.W)) Jump();
            if (Input.GetKeyDown(KeyCode.S)) GetDown();
            if (Input.GetKeyDown(KeyCode.A)) Defense();
            if (Input.GetKeyDown(KeyCode.D)) Attack();
    }

現在很多電腦遊戲都有自定義按鍵的功能,這裏通過unity實現一個簡單的配置按鍵的功能:

思路

一般玩家修改按鍵的邏輯如下:

這裏寫圖片描述

界面設計

這裏寫圖片描述

實現效果

  1. 初始界面如上圖:
      
  2. 點擊按鍵區域後
    這裏寫圖片描述
  3. 再按下F鍵後
      
    這裏寫圖片描述

主要代碼

public class CustomInput : MonoBehaviour
{
    //assign values by inspector
    public Button jumpBtn;
    public Button getDownBtn;
    public Button defBtn;
    public Button atkBtn;
    //bind keys
    KeyCode jump_key;
    KeyCode getdown_key;
    KeyCode def_key;
    KeyCode atk_key;
    //temp current key
    KeyCode curKey;
    //new key
    KeyCode newKey;
    //current function
    string functionName = string.Empty;
    //clicked button
    Button currentBtn; 

    void Awake()
    {
        GetBindKeys();
    }
    // Use this for initialization
    void Start()
    {
        jumpBtn.onClick.AddListener(JumpBtnClick);
        getDownBtn.onClick.AddListener(GetDownBtnClick);
        defBtn.onClick.AddListener(DefBtnClick);
        atkBtn.onClick.AddListener(AtkBtnClick);
    }
    //when you are setting the input,update will be disabled
    bool isPlaying = true;
    // Update is called once per frame
    void Update()
    {
        if(isPlaying)
        {
            if (Input.GetKeyDown(jump_key)) Jump();
            if (Input.GetKeyDown(getdown_key)) GetDown();
            if (Input.GetKeyDown(def_key)) Defense();
            if (Input.GetKeyDown(atk_key)) Attack();
        }
    }

    bool isWaitingForKey = false;
    void OnGUI()
    {
        if (isWaitingForKey)
        {
            Event e = Event.current;
            if (e.isKey)
            {
                newKey = e.keyCode;
                currentBtn.transform.Find("Text").GetComponent<Text>().text = newKey.ToString();
                PlayerPrefs.SetString(functionName, newKey.ToString());
                isWaitingForKey = false;
                //call when config has changed
                GetBindKeys();
                StartCoroutine(WaitUpdate());
            }
        }
    }

    IEnumerator WaitUpdate()
    {
        yield return new WaitForEndOfFrame();
        isPlaying = true;
    }
    //onclick
    void JumpBtnClick()
    {
        currentBtn = jumpBtn;
        isPlaying = false;
        currentBtn.transform.Find("Text").GetComponent<Text>().text = "";
        curKey = (KeyCode)System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("jump","W"));
        functionName = "jump";
        isWaitingForKey = true;
    }
    void GetDownBtnClick()
    {
        currentBtn = getDownBtn;
        isPlaying = false;
        currentBtn.transform.Find("Text").GetComponent<Text>().text = "";
        curKey = (KeyCode)System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("getdown", "S"));
        functionName = "getdown";
        isWaitingForKey = true;
    }
    void DefBtnClick()
    {
        currentBtn = defBtn;
        isPlaying = false;
        currentBtn.transform.Find("Text").GetComponent<Text>().text = "";
        curKey = (KeyCode)System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("defense", "D"));
        functionName = "defense";
        isWaitingForKey = true;
    }
    void AtkBtnClick()
    {
        currentBtn = atkBtn;
        isPlaying = false;
        currentBtn.transform.Find("Text").GetComponent<Text>().text = "";
        curKey = (KeyCode)System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("attack", "A"));
        functionName = "attack";
        isWaitingForKey = true;
    }
    //detail
    void Jump()
    {
        Debug.Log("我跳了!");
    }
    void GetDown()
    {
        Debug.Log("我趴下了!");
    }
    void Defense()
    {
        Debug.Log("我防禦了!");
    }
    void Attack()
    {
        Debug.Log("我攻擊了!");
    }
    /// <summary>
    /// get and parse playerprefs
    /// </summary>
    void GetBindKeys()
    {
        jump_key = (KeyCode)System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("jump", "W"));
        getdown_key = (KeyCode)System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("getdown", "S"));
        def_key = (KeyCode)System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("defense", "D"));
        atk_key = (KeyCode)System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("attack", "A"));
        //setting panel display
        jumpBtn.transform.Find("Text").GetComponent<Text>().text = PlayerPrefs.GetString("jump", "W");
        getDownBtn.transform.Find("Text").GetComponent<Text>().text = PlayerPrefs.GetString("getdown", "S");
        defBtn.transform.Find("Text").GetComponent<Text>().text = PlayerPrefs.GetString("defense", "D");
        atkBtn.transform.Find("Text").GetComponent<Text>().text = PlayerPrefs.GetString("attack", "A");
    }
}

不足之處:沒有進行按鍵衝突判斷
github:https://github.com/Ayosi1996/Unity_Custom_Input

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章