#Struggling with writing comments
1 messages · Page 1 of 1 (latest)
using Newtonsoft.Json; // Importing the Newtonsoft.Json library.
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.Events;
/// <summary>
/// A unity event which is called when a keybind changes keycode.
/// </summary>
[Serializable]
public class KeyChanged : UnityEvent<KeyCode, PlayerAction>
{
}
public class KeyBinds : MonoBehaviour
{
/// <summary>
/// The PlayerAction that will have its KeyCode replaced.
/// </summary>
[SerializeField]
private PlayerAction changingAction;
/// <summary>
/// When true the next key pressed replaces the keycode bound to the PlayerAction set to changingAction.
/// </summary>
private bool changeAction;
/// <summary>
/// A unity event which is called when a keybind changes keycode.
/// </summary>
public KeyChanged keyChanged;
/// <summary>
/// A dictionary with PlayerAction and KeyCode pairs that
/// </summary>
public Dictionary<PlayerAction, KeyCode> keyBinds;
// Loads keybinds from a save file, if any keycode was missing it saves after adding it.
void Awake()
{
// Try to load keybinds from file. If file does not exist, create a new dictionary.
if (!Load())
{
keyBinds = new Dictionary<PlayerAction, KeyCode>();
}
// Add default keybinds to the dictionary and save them to file if any of them are missing.
if (Add(KeyCode.W, PlayerAction.Forward)|
Add(KeyCode.D, PlayerAction.Right)|
Add(KeyCode.S, PlayerAction.Back)|
Add(KeyCode.A, PlayerAction.Left)|
Add(KeyCode.Space, PlayerAction.PickUpOrDrop)|
Add(KeyCode.Q, PlayerAction.CameraLeft)|
Add(KeyCode.E, PlayerAction.CameraRight)|
Add(KeyCode.Escape, PlayerAction.ToggleMenu))
{
Save();
}
// If the KeyChanged event has not been initialized, create a new one.
if (keyChanged == null)
{
keyChanged = new KeyChanged();
}
}
/// <summary>
/// Save the keybinds to KeyBinds.json.
/// </summary>
private void Save()
{
// Convert the dictionary to JSON format and save it to file.
string data = JsonConvert.SerializeObject(keyBinds);
Debug.Log(data);
string filePath = Application.dataPath + "\\KeyBinds.json";
System.IO.File.WriteAllText(filePath, data);
}
/// <summary>
/// Load the keybinds from KeyBinds.json.
/// </summary>
/// <returns>Returns false when the file doesn't exist.</returns>
private bool Load()
{
// Check if the file exists. If it does, load the keybinds from it and return true. Otherwise, return false.
string filePath = Application.dataPath + "\KeyBinds.json";
if (!System.IO.File.Exists(filePath))
{
return false;
}
string data = System.IO.File.ReadAllText(filePath);
keyBinds = JsonConvert.DeserializeObject<Dictionary<PlayerAction, KeyCode>>(data);
return true;
}
/// <summary>
/// Add a new keybind to the list.
/// </summary>
/// <param name="keyCode">The KeyCode to add.</param>
/// <param name="action">The PlayerAction to be linked with the KeyCode.</param>
/// <returns>Returns false if the PlayerAction or KeyCode already exist in the dictionary.</returns>
private bool Add(KeyCode keyCode, PlayerAction
// If any key bindings were missing, add them and save to the file.
if (Add(KeyCode.W, PlayerAction.Forward) |
Add(KeyCode.D, PlayerAction.Right) |
Add(KeyCode.S, PlayerAction.Back) |
Add(KeyCode.A, PlayerAction.Left) |
Add(KeyCode.Space, PlayerAction.PickUpOrDrop) |
Add(KeyCode.Q, PlayerAction.CameraLeft) |
Add(KeyCode.E, PlayerAction.CameraRight) |
Add(KeyCode.Escape, PlayerAction.ToggleMenu))
{
Save();
}
// If keyChanged event has not been assigned, create a new instance.
if (keyChanged == null)
{
keyChanged = new KeyChanged();
}
}
/// <summary>
/// Save the keybinds to KeyBinds.json.
/// </summary>
private void Save()
{
// Serialize the keybinds into a JSON string.
string data = JsonConvert.SerializeObject(keyBinds);
Debug.Log(data);
// Write the JSON string to the KeyBinds.json file.
string filePath = Application.dataPath + "\\KeyBinds.json";
System.IO.File.WriteAllText(filePath, data);
}
/// <summary>
/// Load the keybinds from KeyBinds.json.
/// </summary>
/// <returns>Returns false when the file doesn't exist.</returns>
private bool Load()
{
// Check if the KeyBinds.json file exists.
string filePath = Application.dataPath + "\KeyBinds.json";
if (!System.IO.File.Exists(filePath))
{
return false;
}
// Read the JSON string from the file and deserialize it into a dictionary.
string data = System.IO.File.ReadAllText(filePath);
keyBinds = JsonConvert.DeserializeObject<Dictionary<PlayerAction, KeyCode>>(data);
return true;
}
/// <summary>
/// Add a new keybind to the list.
/// </summary>
/// <param name="keyCode">The KeyCode to add.</param>
/// <param name="action">The PlayerAction to be linked with the KeyCode.</param>
/// <returns>Returns false if the keybind already exists.</returns>
private bool Add(KeyCode keyCode, PlayerAction action)
{
// Check if the keybind already exists in the dictionary.
if (keyBinds.ContainsKey(action) || keyBinds.ContainsValue(keyCode))
{
return false;
}
// Add the new keybind to the dictionary.
keyBinds.Add(action, keyCode);
return true;
}
// Listen for a keypress and change the keybind if a key is pressed.
void OnGUI()
{
if (Event.current.isKey)
{
Debug.Log(Event.current.keyCode);
if (changeAction)
{
Change(Event.current.keyCode);
}
}
}
/// <summary>
/// Listen for a keypress and change the keybind to the new key.
/// </summary>
/// <param name="action">The PlayerAction to listen for.</param>
public void ChangeKey(PlayerAction action)
{
// Set the flag to true to indicate that a keybind is being changed.
changeAction = true;
changingAction = action;
}
/// <summary>
/// Remove the old keybind and add a new keybind.
/// </summary>
/// <param name="keyCode">The KeyCode for the new keybind.</param>
private void Change(KeyCode keyCode)
{
// Set the flag to false to indicate that the keybind change is complete.
changeAction = false;
// Remove the old keybind and add the new key