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 keyMappings = new Dictionary(); 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 = keyMappings["Forward"].ToString(); aKeyButton.GetComponentInChildren().text = keyMappings["Left"].ToString(); sKeyButton.GetComponentInChildren().text = keyMappings["Backward"].ToString(); dKeyButton.GetComponentInChildren().text = keyMappings["Right"].ToString(); } }