64 lines
2.2 KiB
C#
64 lines
2.2 KiB
C#
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
using System.Collections.Generic;
|
|
|
|
public class KeybindingManager : MonoBehaviour
|
|
{
|
|
public Button wKeyButton;
|
|
public Button aKeyButton;
|
|
public Button sKeyButton;
|
|
public Button dKeyButton;
|
|
|
|
private Dictionary<string, KeyCode> keyMappings = new Dictionary<string, KeyCode>();
|
|
|
|
private string keyToRebind;
|
|
|
|
void Start()
|
|
{
|
|
keyMappings["Forward"] = (KeyCode)System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("ForwardKey", KeyCode.W.ToString()));
|
|
keyMappings["Left"] = (KeyCode)System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("LeftKey", KeyCode.A.ToString()));
|
|
keyMappings["Backward"] = (KeyCode)System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("BackwardKey", KeyCode.S.ToString()));
|
|
keyMappings["Right"] = (KeyCode)System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("RightKey", KeyCode.D.ToString()));
|
|
|
|
UpdateButtonTexts();
|
|
wKeyButton.onClick.AddListener(() => StartRebinding("Forward"));
|
|
aKeyButton.onClick.AddListener(() => StartRebinding("Left"));
|
|
sKeyButton.onClick.AddListener(() => StartRebinding("Backward"));
|
|
dKeyButton.onClick.AddListener(() => StartRebinding("Right"));
|
|
}
|
|
|
|
void Update()
|
|
{
|
|
|
|
}
|
|
|
|
public void StartRebinding(string key)
|
|
{
|
|
keyToRebind = key;
|
|
Debug.Log("Naciœnij nowy klawisz dla: " + key);
|
|
}
|
|
|
|
void OnGUI()
|
|
{
|
|
if (!string.IsNullOrEmpty(keyToRebind))
|
|
{
|
|
Event e = Event.current;
|
|
if (e.isKey)
|
|
{
|
|
keyMappings[keyToRebind] = e.keyCode;
|
|
PlayerPrefs.SetString(keyToRebind + "Key", e.keyCode.ToString());
|
|
keyToRebind = null;
|
|
UpdateButtonTexts();
|
|
}
|
|
}
|
|
}
|
|
|
|
private void UpdateButtonTexts()
|
|
{
|
|
wKeyButton.GetComponentInChildren<Text>().text = keyMappings["Forward"].ToString();
|
|
aKeyButton.GetComponentInChildren<Text>().text = keyMappings["Left"].ToString();
|
|
sKeyButton.GetComponentInChildren<Text>().text = keyMappings["Backward"].ToString();
|
|
dKeyButton.GetComponentInChildren<Text>().text = keyMappings["Right"].ToString();
|
|
}
|
|
}
|