- Home /
I want to create a custom universal reference like the built in Unity Input class
So i created this small custom KeyInput Class that i intend to use for the game to provide a better gameplay with broken controllers.....kind of a stretch but accessiblity is a big part of my game design so...yeah....anyways...as of now....i have to define a KeyInput variable in every script and then call it like..
//for example heal is my KeyInput variable
if(heal.Down())
{
//Do Something
}
I want to make a Input manager of sorts in which i can make custom KeyInput variables universally. Something like how Color.red and Vecor3.forward...but just make it a MonoBehaviour script so that i can implement customising controls..... that i can call simply like
if(GI.heal.Down())
{
//Do Something
}
or maybe create a custom function within the GI in which i can pass a string and use it similarly to the basic Old input system.....my question is...how do i create this GI thing? Im an intermediate in unity and i don't really know how to do these sorts of things.....I know it may involve creating directives like using UnityEngine and stuff...I couldn't find any useful resource online about this...but if you do know any..please tell me....thanks in advance
Answer by Captain_Pineapple · Jul 28, 2021 at 05:25 PM
The magic keyword you are searching for is static
if you have a class like this:
public class KeyManager {
public static KeyCode HealKey = KeyCode.Enter;
public static bool Down(KeyCode key)
{
return Input.GetKeyDown(key);
}
}
you can access this by KeyManager.Down(KeyManager.HealKey)
. (imo you should scrap the extra wrapper for Input.KeyDown here and just write Input.KeyDown(KeyManager.HealKey)
directly)
alternative:
define yourself an enum
like this:
public enum Actions{ Heal, Move, Jump};
and then have a class like this:
public class KeyManager {
private static Dictionary<Actions, KeyCode> ActionKeyMap;
public static bool Down(Actions action)
{
return Input.GetKeyDown(ActionKeyMap[action]);
}
}
The latter has the advantage that you have an easier time to test things automatically as you can for example loop over all possible key assignements. If you choose option one where you have to define a variable for each key to hold the keycode you would have to add each key assignement individually to any kind of test.
I wrote this lil script to work with my KeyInput system here, but how do i assign keys to the keys array through this?...This is my GI script...im sorry if i missed something basic here or didn't understand your answer at all
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
I use c# and have a problem about namespace System.Drawing! 3 Answers
Distribute terrain in zones 3 Answers
Detect if running on mobile vs desktop 2 Answers
(C#) A better way to limit actions to once per button press? 2 Answers