- Home /
Custom Input Manager Menu Reset Keybinds
I was Wondering if anyone could help me out with getting my Input Manager to reset to the default keybind when i click a button in the scene this is what i have current
the main reason i need to set this up is because for what ever reason i can not set mouse 0,1 or either of the shifts in the game and i am not sure why
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class GameManager : MonoBehaviour
{
public static GameManager gameManager;
public KeyCode jump { get; set; }
public KeyCode forward { get; set; }
public KeyCode backward { get; set; }
public KeyCode left { get; set; }
public KeyCode right { get; set; }
public KeyCode fire { get; set; }
public KeyCode aimdown { get; set; }
public KeyCode sprint { get; set; }
public KeyCode weaponswap { get; set; }
public KeyCode crouch { get; set; }
public KeyCode interact { get; set; }
public KeyCode drop { get; set; }
public KeyCode inventory { get; set; }
public KeyCode ingamemenu { get; set; }
public KeyCode equipment { get; set; }
public KeyCode reload { get; set; }
void Awake()
{
if (gameManager == null)
{
DontDestroyOnLoad(gameObject);
gameManager = this;
}
else if (gameManager != this)
{
Destroy(gameObject);
}
jump = (KeyCode)System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("jumpKey", "Space"));
forward = (KeyCode)System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("forwardKey", "W"));
backward = (KeyCode)System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("backwardKey", "S"));
left = (KeyCode)System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("leftKey", "A"));
right = (KeyCode)System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("rightKey", "D"));
fire = (KeyCode)System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("shootKey", "Mouse0"));
aimdown = (KeyCode)System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("aimdownKey", "Mouse1"));
sprint = (KeyCode)System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("sprintKey", "LeftShift"));
weaponswap = (KeyCode)System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("weaponswapKey", "1"));
crouch = (KeyCode)System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("crouchKey", "LeftControl"));
interact = (KeyCode)System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("interactKey", "E"));
drop = (KeyCode)System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("dropKey", "G"));
inventory = (KeyCode)System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("InventoryKey", "I"));
ingamemenu = (KeyCode)System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("ingamemenuKey", "Escape"));
equipment = (KeyCode)System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("equipmentKey", "J"));
reload = (KeyCode)System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("reloadKey", "R"));
}
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
}
public void ResetDefault()
{
jump = (KeyCode)System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("jumpKey", "Space"));
forward = (KeyCode)System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("forwardKey", "W"));
backward = (KeyCode)System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("backwardKey", "S"));
left = (KeyCode)System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("leftKey", "A"));
right = (KeyCode)System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("rightKey", "D"));
fire = (KeyCode)System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("shootKey", "Mouse0"));
aimdown = (KeyCode)System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("aimdownKey", "Mouse1"));
sprint = (KeyCode)System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("sprintKey", "LeftShift"));
weaponswap = (KeyCode)System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("weaponswapKey", "1"));
crouch = (KeyCode)System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("crouchKey", "LeftControl"));
interact = (KeyCode)System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("interactKey", "E"));
drop = (KeyCode)System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("dropKey", "G"));
inventory = (KeyCode)System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("InventoryKey", "I"));
ingamemenu = (KeyCode)System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("ingamemenuKey", "Escape"));
equipment = (KeyCode)System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("equipmentKey", "J"));
reload = (KeyCode)System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString("reloadKey", "R"));
}
}
this is my gameManager where everything is set the ResetDefault currently just locks me out of the game
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class MenuScript : MonoBehaviour
{
public Transform menuPanel;
Event keyEvent;
Text buttonText;
KeyCode newKey;
bool waitingForKey;
// Use this for initialization
void Start ()
{
//menuPanel = transform.FindChild("KeyBinds Content");
waitingForKey = false;
for(int i = 0; i < menuPanel.childCount; i++)
{
if (menuPanel.GetChild(i).name == "ForwardKey")
menuPanel.GetChild(i).GetComponentInChildren<Text>().text = GameManager.gameManager.forward.ToString();
else if (menuPanel.GetChild(i).name == "BackKey")
menuPanel.GetChild(i).GetComponentInChildren<Text>().text = GameManager.gameManager.backward.ToString();
else if (menuPanel.GetChild(i).name == "LeftKey")
menuPanel.GetChild(i).GetComponentInChildren<Text>().text = GameManager.gameManager.left.ToString();
else if (menuPanel.GetChild(i).name == "RightKey")
menuPanel.GetChild(i).GetComponentInChildren<Text>().text = GameManager.gameManager.right.ToString();
else if (menuPanel.GetChild(i).name == "JumpKey")
menuPanel.GetChild(i).GetComponentInChildren<Text>().text = GameManager.gameManager.jump.ToString();
else if (menuPanel.GetChild(i).name == "SprintKey")
menuPanel.GetChild(i).GetComponentInChildren<Text>().text = GameManager.gameManager.sprint.ToString();
else if (menuPanel.GetChild(i).name == "CrouchKey")
menuPanel.GetChild(i).GetComponentInChildren<Text>().text = GameManager.gameManager.crouch.ToString();
else if (menuPanel.GetChild(i).name == "FireKey")
menuPanel.GetChild(i).GetComponentInChildren<Text>().text = GameManager.gameManager.fire.ToString();
else if (menuPanel.GetChild(i).name == "AimDownKey")
menuPanel.GetChild(i).GetComponentInChildren<Text>().text = GameManager.gameManager.aimdown.ToString();
else if (menuPanel.GetChild(i).name == "ReloadKey")
menuPanel.GetChild(i).GetComponentInChildren<Text>().text = GameManager.gameManager.reload.ToString();
else if (menuPanel.GetChild(i).name == "WeaponSwapKey")
menuPanel.GetChild(i).GetComponentInChildren<Text>().text = GameManager.gameManager.weaponswap.ToString();
else if (menuPanel.GetChild(i).name == "InteractKey")
menuPanel.GetChild(i).GetComponentInChildren<Text>().text = GameManager.gameManager.interact.ToString();
else if (menuPanel.GetChild(i).name == "DropKey")
menuPanel.GetChild(i).GetComponentInChildren<Text>().text = GameManager.gameManager.drop.ToString();
else if (menuPanel.GetChild(i).name == "InventoryKey")
menuPanel.GetChild(i).GetComponentInChildren<Text>().text = GameManager.gameManager.inventory.ToString();
else if (menuPanel.GetChild(i).name == "InGameKey")
menuPanel.GetChild(i).GetComponentInChildren<Text>().text = GameManager.gameManager.ingamemenu.ToString();
else if (menuPanel.GetChild(i).name == "EquipmentKey")
menuPanel.GetChild(i).GetComponentInChildren<Text>().text = GameManager.gameManager.equipment.ToString();
}
}
// Update is called once per frame
void Update ()
{
}
void OnGUI()
{
keyEvent = Event.current;
if(keyEvent.isKey && waitingForKey)
{
newKey = keyEvent.keyCode;
waitingForKey = false;
}
}
public void StartAssigment(string keyName)
{
if(!waitingForKey)
StartCoroutine(AssignKey(keyName));
}
public void SendText(Text text)
{
buttonText = text;
}
IEnumerator WaitForKey()
{
while(!keyEvent.isKey)
yield return null;
}
public IEnumerator AssignKey(string keyName)
{
waitingForKey = true;
yield return WaitForKey();
switch(keyName)
{
case "forward":
GameManager.gameManager.forward = newKey;
buttonText.text = GameManager.gameManager.forward.ToString();
PlayerPrefs.SetString("forwardKey", GameManager.gameManager.forward.ToString());
break;
case "backward":
GameManager.gameManager.backward = newKey;
buttonText.text = GameManager.gameManager.backward.ToString();
PlayerPrefs.SetString("backwardKey", GameManager.gameManager.backward.ToString());
break;
case "left":
GameManager.gameManager.forward = newKey;
buttonText.text = GameManager.gameManager.left.ToString();
PlayerPrefs.SetString("leftKey", GameManager.gameManager.left.ToString());
break;
case "right":
GameManager.gameManager.forward = newKey;
buttonText.text = GameManager.gameManager.right.ToString();
PlayerPrefs.SetString("rightKey", GameManager.gameManager.right.ToString());
break;
case "jump":
GameManager.gameManager.jump = newKey;
buttonText.text = GameManager.gameManager.jump.ToString();
PlayerPrefs.SetString("jumpKey", GameManager.gameManager.jump.ToString());
break;
case "sprint":
GameManager.gameManager.sprint = newKey;
buttonText.text = GameManager.gameManager.sprint.ToString();
PlayerPrefs.SetString("sprintKey", GameManager.gameManager.sprint.ToString());
break;
case "crouch":
GameManager.gameManager.crouch = newKey;
buttonText.text = GameManager.gameManager.crouch.ToString();
PlayerPrefs.SetString("crouchKey", GameManager.gameManager.crouch.ToString());
break;
case "shoot":
GameManager.gameManager.fire = newKey;
buttonText.text = GameManager.gameManager.fire.ToString();
PlayerPrefs.SetString("shootKey", GameManager.gameManager.fire.ToString());
break;
case "aimdown":
GameManager.gameManager.aimdown = newKey;
buttonText.text = GameManager.gameManager.aimdown.ToString();
PlayerPrefs.SetString("aimdownKey", GameManager.gameManager.aimdown.ToString());
break;
case "reload":
GameManager.gameManager.reload = newKey;
buttonText.text = GameManager.gameManager.reload.ToString();
PlayerPrefs.SetString("reloadKey", GameManager.gameManager.reload.ToString());
break;
case "weaponswap":
GameManager.gameManager.weaponswap = newKey;
buttonText.text = GameManager.gameManager.weaponswap.ToString();
PlayerPrefs.SetString("weaponswapKey", GameManager.gameManager.weaponswap.ToString());
break;
case "interact":
GameManager.gameManager.interact = newKey;
buttonText.text = GameManager.gameManager.interact.ToString();
PlayerPrefs.SetString("interactKey", GameManager.gameManager.interact.ToString());
break;
case "drop":
GameManager.gameManager.drop = newKey;
buttonText.text = GameManager.gameManager.drop.ToString();
PlayerPrefs.SetString("dropKey", GameManager.gameManager.drop.ToString());
break;
case "inventory":
GameManager.gameManager.inventory = newKey;
buttonText.text = GameManager.gameManager.inventory.ToString();
PlayerPrefs.SetString("InventoryKey", GameManager.gameManager.inventory.ToString());
break;
case "ingame":
GameManager.gameManager.ingamemenu = newKey;
buttonText.text = GameManager.gameManager.ingamemenu.ToString();
PlayerPrefs.SetString("ingamemenuKey", GameManager.gameManager.ingamemenu.ToString());
break;
case "equipment":
GameManager.gameManager.equipment = newKey;
buttonText.text = GameManager.gameManager.equipment.ToString();
PlayerPrefs.SetString("equipmentKey", GameManager.gameManager.equipment.ToString());
break;
}
yield return null;
}
}
i am not sure if you need to see how i set it up but here is the other script used just in case
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Basic AI Follow Player 2D C# 1 Answer
Save randomly placed objects 0 Answers
Is it safe to read from managed objects when using IParallelFor? 0 Answers