- using System.Collections;
- using System.Collections.Generic;
- using TMPro;
- using Unity.VisualScripting;
- using UnityEditor;
- using UnityEngine;
- using UnityEngine.SceneManagement;
- using UnityEngine.UI;
- public class KeyBindingsManager : MonoBehaviour
- {
- public static KeyBindingsManager Instance { get; private set; }
- public Dictionary<string, KeyCode> keyBindings = new Dictionary<string, KeyCode>();
- public List<TMP_InputField> inputFields;
- KeyBindDictionary keyBindingsDictionary;
- private void Awake()
- {
- if (Instance == null)
- {
- Instance = this;
- DontDestroyOnLoad(gameObject);
- }
- else
- {
- Destroy(gameObject);
- }
- }
- private void Start()
- {
- keyBindings.Add("Jump", KeyCode.Space);
- keyBindings.Add("Forward", KeyCode.D);
- keyBindings.Add("Backward", KeyCode.A);
- foreach (TMP_InputField inputField in inputFields)
- {
- inputField.text = keyBindings[inputField.name].ToString();
- inputField.onValueChanged.AddListener(delegate { OnKeyBindingChanged(inputField); });
- }
-
- }
- private void OnKeyBindingChanged(TMP_InputField inputField)
- {
- if (inputField.text != string.Empty)
- {
- KeyCode keyCode = (KeyCode)System.Enum.Parse(typeof(KeyCode), inputField.text);
- keyBindings[inputField.name] = keyCode;
- }
- else
- {
- inputField.text = keyBindings[inputField.name].ToString();
- }
- }
- public void OnSelect(TMP_InputField inputField)
- {
- inputField.text = "Insert New Key";
- }
- private void Update()
- {
- foreach (KeyCode key in keyBindings.Values)
- {
- Debug.Log("Key: " + key);
- }
- }
- public void ChangeScene()
- {
- SceneManager.LoadScene("RandomCharacter");
- }
- }