unity做单机游戏,按键修改,更换场景或者退出游戏,按键依旧保存

unity做单机游戏,按键修改,更换场景按键依旧保存(做毕业设计过程中的一些心得,第一次发文,有错误的敬请谅解)


定义新按键
KeyCode newKey;
newKey= (KeyCode)System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString(“forward”, “W”));

直接上代码(说明一点,有的东西定义的是public公有类型的变量,需要自己在unity里赋值,最好看一下代码,然后根据条件来使用),

//自定义按键代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ChangeKey : MonoBehaviour {
    //这里本来是能定义按钮的,但是我的Unity出了点毛病,暂时这样了
    public GameObject  forward;
    public GameObject back;
    public GameObject left;
    public GameObject right;
    public GameObject attack;
    public GameObject skill1;
    public GameObject skill2;
    public GameObject skill3;
    //定义的新按键
    KeyCode backkey;
    KeyCode forwardkey;
    KeyCode rightkey;
    KeyCode leftkey;
    KeyCode attackkey;
    KeyCode skill1key;
    KeyCode skill2key;
    KeyCode skill3key;
   //临时按键
    KeyCode curKey;
   //新的按键
    KeyCode newKey;
    //存储按键的事件名称
    string functionName = string.Empty;
    //临时按钮
    GameObject  currentBtn;
    void Awake()
    {
        GetBindKeys();
    }
    bool isPlaying = true;
    bool isWaitingForKey = false;
    //切换按键的输入与显示
    void OnGUI()
    {
        if (isWaitingForKey)
        {
            Event e = Event.current;
            if (e.isKey)
            {
                newKey = e.keyCode;
                currentBtn.transform.Find("Text").GetComponent().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;
    }
    //按钮事件,需要给按钮手动添加事件
    public  void ForwardBtnClick()
    {
        currentBtn = forward;
        isPlaying = false;
        currentBtn.transform.Find("Text").GetComponent().text = "";
        curKey = (KeyCode)System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("forward", "W"));
        functionName = "forward";
        isWaitingForKey = true;
    }
   public  void BackBtnClick()
    {
        currentBtn = back;
        isPlaying = false;
        currentBtn.transform.Find("Text").GetComponent().text = "";
        curKey = (KeyCode)System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("back", "S"));
        functionName = "back";
        isWaitingForKey = true;
    }
  public   void RightBtnClick()
    {
        currentBtn = right;
        isPlaying = false;
        currentBtn.transform.Find("Text").GetComponent().text = "";
        curKey = (KeyCode)System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("right", "D"));
        functionName = "right";
        isWaitingForKey = true;
    }
  public   void LeftBtnClick()
    {
        currentBtn = left;
        isPlaying = false;
        currentBtn.transform.Find("Text").GetComponent().text = "";
        curKey = (KeyCode)System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("left", "A"));
        functionName = "left";
        isWaitingForKey = true;
    }
    public void AttackBtnClick()
    {
        currentBtn = attack ;
        isPlaying = false;
        currentBtn.transform.Find("Text").GetComponent().text = "";
        curKey = (KeyCode)System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("attack", "I"));
        functionName = "attack";
        isWaitingForKey = true;
    }
    public void Skill1BtnClick()
    {
        currentBtn = skill1;
        isPlaying = false;
        currentBtn.transform.Find("Text").GetComponent().text = "";
        curKey = (KeyCode)System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("skill1", "J"));
        functionName = "skill1";
        isWaitingForKey = true;
    }
    public void Skill2BtnClick()
    {
        currentBtn = skill2;
        isPlaying = false;
        currentBtn.transform.Find("Text").GetComponent().text = "";
        curKey = (KeyCode)System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("skill2", "K"));
        functionName = "skill2";
        isWaitingForKey = true;
    }
    public void Skill3BtnClick()
    {
        currentBtn = skill3;
        isPlaying = false;
        currentBtn.transform.Find("Text").GetComponent().text = "";
        curKey = (KeyCode)System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("skill3", "L"));
        functionName = "skill3";
        isWaitingForKey = true;
    }
    //绑定按键的值
    void GetBindKeys()
    {
        forwardkey  = (KeyCode)System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("forward", "W"));
        backkey  = (KeyCode)System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("back", "S"));
        rightkey  = (KeyCode)System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("right", "D"));
        leftkey  = (KeyCode)System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("left", "A"));
        attackkey = (KeyCode)System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("attack", "I"));
        skill1key = (KeyCode)System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("skill1", "J"));
        skill2key = (KeyCode)System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("skill2", "K"));
        skill3key = (KeyCode)System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("skill3", "L"));

        forward.transform.Find("Text").GetComponent().text = PlayerPrefs.GetString("forward", "W");
        back.transform.Find("Text").GetComponent().text = PlayerPrefs.GetString("back", "S");
        right.transform.Find("Text").GetComponent().text = PlayerPrefs.GetString("right", "D");
        left.transform.Find("Text").GetComponent().text = PlayerPrefs.GetString("left", "A");
        attack .transform.Find("Text").GetComponent().text = PlayerPrefs.GetString("attack", "I");
        skill1.transform.Find("Text").GetComponent().text = PlayerPrefs.GetString("skill1", "J");
        skill2.transform.Find("Text").GetComponent().text = PlayerPrefs.GetString("skill2", "K");
        skill3.transform.Find("Text").GetComponent().text = PlayerPrefs.GetString("skill3", "L");
    }
}
``还是需要根据条件来使用(这是我毕设中的攻击与技能更改按键的实现)

//修改的按键的使用
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class anima : MonoBehaviour {
    private Animator anim;
    private Rigidbody rig;
    KeyCode attack;
    KeyCode skill1;
    KeyCode skill2;
    KeyCode skill3;
    // Use this for initialization
    void Start () {
        anim = this.GetComponent();
        rig = this.GetComponent();
    }

    // Update is called once per frame
    void Update () {
        //print(PlayerPrefs.GetString("attack"));

        attack= (KeyCode)System.Enum.Parse(typeof(KeyCode), PlayerPrefs .GetString ("attack"));
        skill1  = (KeyCode)System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("skill1"));
        skill2 = (KeyCode)System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("skill2"));
        skill3 = (KeyCode)System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("skill3"));
        if (Input.GetKeyDown(attack))
        {
            print("Show");
            anim.SetTrigger("Attack");
        }
        if (Input.GetKeyDown(skill1 ))
        {
            anim.SetBool("Skill1", true);
        }
        else
        {
            anim.SetBool("Skill1", false);
        }
        if (Input.GetKeyDown(skill2 ))
        {
            anim.SetBool ("Skill2",true );
        }
        else
        {
            anim.SetBool("Skill2", false);
        }
        if (Input.GetKeyDown(skill3))
        {
            anim.SetBool("Skill3", true);
        }
        else
        {
            anim.SetBool("Skill3", false);
        }
        if (Input.GetKeyDown(KeyCode.Space))
        {
            rig.isKinematic = false;

            for (int j = 0; j < TranscriptManager._instance.enemyList.Count; j++)
            {
                transform.LookAt(TranscriptManager._instance.enemyList[j].transform .position );
            }
        }
    }
}

你可能感兴趣的:(unity做游戏中的一些心得)