2023-01-02 00:37:49 +01:00
|
|
|
using System.Collections;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using UnityEngine;
|
|
|
|
using UnityEngine.UI;
|
|
|
|
|
|
|
|
public class KeyBindScript : MonoBehaviour
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// This is the dictionary, that contains all our keybinds
|
|
|
|
/// </summary>
|
|
|
|
private Dictionary<string, KeyCode> keys = new Dictionary<string, KeyCode>();
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// References to the button objects, these are used for showing the current keybinds
|
|
|
|
/// </summary>
|
2023-01-10 04:21:36 +01:00
|
|
|
public Text interaction, skills, settings, attack, inventory, quests, exphlth;
|
2023-01-02 00:37:49 +01:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// The key, that we are binding atm.
|
|
|
|
/// </summary>
|
|
|
|
private GameObject currentKey;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// The normal color of the buttons
|
|
|
|
/// </summary>
|
|
|
|
private Color32 normal = new Color32(255, 255, 255, 255);
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// The highlighted color of the buttons
|
|
|
|
/// </summary>
|
|
|
|
private Color32 slected = new Color32(239, 116, 36, 255);
|
|
|
|
|
|
|
|
// Use this for initialization
|
2023-01-05 21:19:05 +01:00
|
|
|
void Start()
|
2023-01-02 00:37:49 +01:00
|
|
|
{
|
2023-01-10 04:21:36 +01:00
|
|
|
keys.Add("Interaction",(KeyCode)System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("Interaction","E")));
|
2023-01-05 14:25:32 +01:00
|
|
|
keys.Add("Settings",(KeyCode)System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("Settings","Escape")));
|
2023-01-02 00:37:49 +01:00
|
|
|
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")));
|
2023-01-07 12:02:47 +01:00
|
|
|
keys.Add("Attack",(KeyCode)System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("Attack","Space")));
|
2023-01-10 04:21:36 +01:00
|
|
|
keys.Add("Quests",(KeyCode)System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("Quests","Q")));
|
|
|
|
keys.Add("ExpHlth",(KeyCode)System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("ExpHlth","Tab")));
|
2023-01-02 00:37:49 +01:00
|
|
|
|
|
|
|
//Sets the text on the buttons
|
2023-01-07 02:56:38 +01:00
|
|
|
//interaction.text = keys["Interact"].ToString();
|
2023-01-10 04:21:36 +01:00
|
|
|
interaction.text = PlayerPrefs.GetString("Interaction");
|
2023-01-07 02:56:38 +01:00
|
|
|
//skills.text = keys["Settings"].ToString();
|
|
|
|
skills.text = PlayerPrefs.GetString("Skills");
|
|
|
|
//settings.text = keys["Inventory"].ToString();
|
|
|
|
settings.text = PlayerPrefs.GetString("Settings");
|
|
|
|
//attack.text = keys["Skills"].ToString();
|
|
|
|
attack.text = PlayerPrefs.GetString("Attack");
|
|
|
|
//inventory.text = keys["Attack"].ToString();
|
|
|
|
inventory.text = PlayerPrefs.GetString("Inventory");
|
2023-01-10 04:21:36 +01:00
|
|
|
quests.text = PlayerPrefs.GetString("Quests");
|
|
|
|
exphlth.text = PlayerPrefs.GetString("ExpHlth");
|
|
|
|
exphlth.text = PlayerPrefs.GetString("EXP HEALTH");
|
2023-01-02 00:37:49 +01:00
|
|
|
}
|
|
|
|
|
2023-01-10 04:21:36 +01:00
|
|
|
void OnGUI()
|
2023-01-02 00:37:49 +01:00
|
|
|
{
|
|
|
|
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>().text = e.keyCode.ToString(); //Sets the text
|
|
|
|
currentKey.GetComponent<Image>().color = normal; //Sets thecolor
|
|
|
|
currentKey = null; //Deselects the key
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Changes a keybind
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="clicked">The clicked button</param>
|
|
|
|
public void ChangeKey(GameObject clicked)
|
|
|
|
{
|
|
|
|
if (currentKey != null) //If we have a selected key, then we need to deselect
|
|
|
|
{
|
|
|
|
currentKey.GetComponent<Image>().color = normal; //Sets the color to normal
|
|
|
|
}
|
|
|
|
|
|
|
|
currentKey = clicked; //Sets the new clicked key as the current key
|
|
|
|
|
|
|
|
currentKey.GetComponent<Image>().color = slected; //Set the color as selected
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Saves the new keybinds
|
|
|
|
/// </summary>
|
|
|
|
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
|
2023-01-10 04:21:36 +01:00
|
|
|
Debug.Log(key.Key + " : " + key.Value.ToString());
|
|
|
|
//Debug.Log(key.Value.ToString());
|
2023-01-02 00:37:49 +01:00
|
|
|
}
|
|
|
|
PlayerPrefs.Save();
|
2023-01-10 04:21:36 +01:00
|
|
|
|
2023-01-07 02:56:38 +01:00
|
|
|
InventoryUIManager.Instance.keyToOpen = (KeyCode) System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("Inventory"));
|
|
|
|
EquipmentUIManager.Instance.keyToOpen = (KeyCode) System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("Inventory"));
|
2023-01-10 04:21:36 +01:00
|
|
|
ChestUIManager.Instance.keyToOpen = (KeyCode) System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("Interaction"));
|
|
|
|
ShopUIManager.Instance.keyToOpen = (KeyCode) System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("Interaction"));
|
2023-01-07 02:56:38 +01:00
|
|
|
SkillsUIManager.Instance.keyToOpen = (KeyCode) System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("Skills"));
|
2023-01-10 04:21:36 +01:00
|
|
|
DisplayExpStatus.keyToExp = (KeyCode) System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("EXP HEALTH"));
|
|
|
|
DisplayHealthStatus.keyToHealth = (KeyCode) System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("EXP HEALTH"));
|
|
|
|
TaskUIManager.Instance.keyToOpen = (KeyCode) System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("Quests"));
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void SaveKeysOut()
|
|
|
|
{
|
|
|
|
InventoryUIManager.Instance.keyToOpen = (KeyCode) System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("Inventory"));
|
|
|
|
EquipmentUIManager.Instance.keyToOpen = (KeyCode) System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("Inventory"));
|
|
|
|
ChestUIManager.Instance.keyToOpen = (KeyCode) System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("Interaction"));
|
|
|
|
ShopUIManager.Instance.keyToOpen = (KeyCode) System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("Interaction"));
|
|
|
|
SkillsUIManager.Instance.keyToOpen = (KeyCode) System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("Skills"));
|
|
|
|
DisplayExpStatus.keyToExp = (KeyCode) System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("EXP HEALTH"));
|
|
|
|
DisplayHealthStatus.keyToHealth = (KeyCode) System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("EXP HEALTH"));
|
|
|
|
TaskUIManager.Instance.keyToOpen = (KeyCode) System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("Quests"));
|
|
|
|
PlayerPrefs.Save();
|
2023-01-02 00:37:49 +01:00
|
|
|
|
2023-01-05 21:19:05 +01:00
|
|
|
}
|
|
|
|
}
|