teraz
This commit is contained in:
parent
e8903517e4
commit
3b955d8aae
@ -492,7 +492,6 @@ GameObject:
|
||||
- component: {fileID: 963194228}
|
||||
- component: {fileID: 963194227}
|
||||
- component: {fileID: 963194226}
|
||||
- component: {fileID: 963194229}
|
||||
m_Layer: 0
|
||||
m_Name: Main Camera
|
||||
m_TagString: MainCamera
|
||||
@ -574,19 +573,6 @@ Transform:
|
||||
m_Children: []
|
||||
m_Father: {fileID: 0}
|
||||
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
|
||||
--- !u!114 &963194229
|
||||
MonoBehaviour:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
m_GameObject: {fileID: 963194225}
|
||||
m_Enabled: 1
|
||||
m_EditorHideFlags: 0
|
||||
m_Script: {fileID: 11500000, guid: 6b9a5d2dd94a0bf459d33130af0ceb69, type: 3}
|
||||
m_Name:
|
||||
m_EditorClassIdentifier:
|
||||
sensitivitySlider: {fileID: 0}
|
||||
--- !u!1 &998624921
|
||||
GameObject:
|
||||
m_ObjectHideFlags: 0
|
||||
|
@ -1,6 +1,7 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
|
||||
public class KeybindingManager : MonoBehaviour
|
||||
{
|
||||
@ -10,54 +11,127 @@ public class KeybindingManager : MonoBehaviour
|
||||
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()
|
||||
{
|
||||
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()));
|
||||
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œ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();
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user