UnityBiz/Assets/scripts/GUI/GameplayUI/KeybindingManager.cs
2024-08-23 01:03:47 +02:00

138 lines
4.5 KiB
C#
Raw Blame History

using UnityEngine;
using UnityEngine.UI;
using System.Collections.Generic;
using System.IO;
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;
private string filePath;
private bool awaitingKeyInput;
[System.Serializable]
public class KeyMappingData
{
public string Forward;
public string Left;
public string Backward;
public string Right;
}
void Start()
{
string folderName = "KeybindsSave";
string folderPath = Path.Combine(Application.persistentDataPath, folderName);
if (!Directory.Exists(folderPath))
{
Directory.CreateDirectory(folderPath);
}
filePath = Path.Combine(folderPath, "keybindings.json");
Debug.Log("Path to keybindings.json: " + filePath);
LoadKeyMappings();
UpdateButtonTexts();
wKeyButton.onClick.AddListener(() => StartRebinding("Forward"));
aKeyButton.onClick.AddListener(() => StartRebinding("Left"));
sKeyButton.onClick.AddListener(() => StartRebinding("Backward"));
dKeyButton.onClick.AddListener(() => StartRebinding("Right"));
wKeyButton.interactable = true;
aKeyButton.interactable = true;
sKeyButton.interactable = true;
dKeyButton.interactable = true;
}
void Update()
{
if (awaitingKeyInput)
{
if (Input.anyKeyDown)
{
foreach (KeyCode key in System.Enum.GetValues(typeof(KeyCode)))
{
if (Input.GetKeyDown(key))
{
if (keyMappings.ContainsKey(keyToRebind))
{
keyMappings[keyToRebind] = key;
SaveKeyMappings();
keyToRebind = null;
awaitingKeyInput = false;
UpdateButtonTexts();
Debug.Log("Przypisano nowy klawisz: " + keyToRebind + " -> " + key);
}
break;
}
}
}
}
}
public void StartRebinding(string key)
{
keyToRebind = key;
awaitingKeyInput = true;
Debug.Log("Naci<63>nij nowy klawisz dla: " + key);
}
private void UpdateButtonTexts()
{
if (keyMappings.ContainsKey("Forward"))
wKeyButton.GetComponentInChildren<Text>().text = keyMappings["Forward"].ToString();
if (keyMappings.ContainsKey("Left"))
aKeyButton.GetComponentInChildren<Text>().text = keyMappings["Left"].ToString();
if (keyMappings.ContainsKey("Backward"))
sKeyButton.GetComponentInChildren<Text>().text = keyMappings["Backward"].ToString();
if (keyMappings.ContainsKey("Right"))
dKeyButton.GetComponentInChildren<Text>().text = keyMappings["Right"].ToString();
}
private void SaveKeyMappings()
{
KeyMappingData data = new KeyMappingData
{
Forward = keyMappings["Forward"].ToString(),
Left = keyMappings["Left"].ToString(),
Backward = keyMappings["Backward"].ToString(),
Right = keyMappings["Right"].ToString()
};
string json = JsonUtility.ToJson(data, true);
File.WriteAllText(filePath, json);
Debug.Log("Zapisano przypisania klawiszy do pliku: " + filePath);
}
private void LoadKeyMappings()
{
if (File.Exists(filePath))
{
string json = File.ReadAllText(filePath);
KeyMappingData data = JsonUtility.FromJson<KeyMappingData>(json);
keyMappings["Forward"] = (KeyCode)System.Enum.Parse(typeof(KeyCode), data.Forward);
keyMappings["Left"] = (KeyCode)System.Enum.Parse(typeof(KeyCode), data.Left);
keyMappings["Backward"] = (KeyCode)System.Enum.Parse(typeof(KeyCode), data.Backward);
keyMappings["Right"] = (KeyCode)System.Enum.Parse(typeof(KeyCode), data.Right);
Debug.Log("Wczytano przypisania klawiszy z pliku: " + filePath);
}
else
{
keyMappings["Forward"] = KeyCode.W;
keyMappings["Left"] = KeyCode.A;
keyMappings["Backward"] = KeyCode.S;
keyMappings["Right"] = KeyCode.D;
}
}
}