using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class KeyBindScript : MonoBehaviour { /// /// This is the dictionary, that contains all our keybinds /// private Dictionary keys = new Dictionary(); /// /// References to the button objects, these are used for showing the current keybinds /// public Text interaction, skills, settings, attack, inventory; /// /// The key, that we are binding atm. /// private GameObject currentKey; /// /// The normal color of the buttons /// private Color32 normal = new Color32(255, 255, 255, 255); /// /// The highlighted color of the buttons /// private Color32 slected = new Color32(239, 116, 36, 255); // Use this for initialization void Start() { keys.Add("Interact",(KeyCode)System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("Interact","E"))); keys.Add("Settings",(KeyCode)System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("Settings","Escape"))); keys.Add("Inventory",(KeyCode)System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("Inventory","I"))); keys.Add("Skills",(KeyCode)System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("Skills","U"))); keys.Add("Attack",(KeyCode)System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("Attack","Space"))); //Sets the text on the buttons interaction.text = keys["Interact"].ToString(); skills.text = keys["Settings"].ToString(); settings.text = keys["Inventory"].ToString(); attack.text = keys["Skills"].ToString(); inventory.text = keys["Attack"].ToString(); } /* // Update is called once per frame void Update() { //Simulates movement and usage of the keys if ( ) { //Do a move action Debug.Log("Up"); } if (Input.GetKeyDown(keys["Down"])) { //Do a move action Debug.Log("Down"); } if (Input.GetKeyDown(keys["Left"])) { //Do a move action Debug.Log("Left"); } if (Input.GetKeyDown(keys["Right"])) { //Do a move action Debug.Log("Right"); } if (Input.GetKeyDown(keys["Jump"])) { //Do a move action Debug.Log("Jump"); } } }*/ void OnGUI() { if (currentKey != null) //If we have selected a key, that we want to edit { Event e = Event.current; //Stores the event if (e.isKey) //If we pressed a key on the keyboard, then we need to assign it to the selected key { keys[currentKey.name] = e.keyCode; //Sets the current key currentKey.transform.GetChild(0).GetComponent().text = e.keyCode.ToString(); //Sets the text currentKey.GetComponent().color = normal; //Sets thecolor currentKey = null; //Deselects the key } } } /// /// Changes a keybind /// /// The clicked button public void ChangeKey(GameObject clicked) { if (currentKey != null) //If we have a selected key, then we need to deselect { currentKey.GetComponent().color = normal; //Sets the color to normal } currentKey = clicked; //Sets the new clicked key as the current key currentKey.GetComponent().color = slected; //Set the color as selected } /// /// Saves the new keybinds /// public void SaveKeys() { foreach (var key in keys) //Runs through the dictionary { PlayerPrefs.SetString(key.Key, key.Value.ToString()); //Saves the keybinds for each key } PlayerPrefs.Save(); } }