Ok I really tried to understand the new Input system but I really find it difficult to migrate my code to this new system.
I don’t want to change everything in my code. Since I made a rebind custom system I should only need to change one class and nothing else. But I think the new Input system works in a really different (and confusing way) because I cannot adapt anything.
My main problems:
- I have two json where I save my current bindings the first is
{"axis":["VerticalJoy","HorizontalJoy","VerticalJoy2","HorizontalJoy2","Mouse X","Mouse Y","Mouse ScrollWheel"]}
the second one is:
{"buttons":["W","S","A","D","Space","Escape","LeftShift","Mouse0","Mouse1","JoystickButton0","JoystickButton7","I","JoystickButton6"]}
In my code I convert my second one to a Dictionary
private static Dictionary<string, KeyCode> inputs = new Dictionary<string, KeyCode>();
private string[] inputList = { "VerticalUp", "VerticalDown", "HorizontalLeft", "HorizontalRight",
"Jump", "Cancel", "Walk", "MouseClick", "MouseRightClick", "JumpJoy",
"CancelJoy", "Inventary", "InventaryJoy" };
SetInput(inputList[j], (KeyCode) System.Enum.Parse(typeof(KeyCode), loadConfirmA[j]));
SetInput
inputs[keyName] = input;
with this kind of code I associate string to a KeyCode… After that I just call MyClass.GetKey(KeyCode) and I return if that KeyCode was pressed
public static bool GetKey(KeyCode code) {
return Input.GetKey(code);
}
public static bool GetKeyDown(KeyCode code) {
return Input.GetKeyDown(code);
}
public static bool GetKeyDown(String code) {
return Input.GetKeyDown(code);
}
public static bool GetKeyUp(KeyCode code) {
return Input.GetKeyUp(code);
}
How can I convert Input.GetKey(KeyCode) in the new Input System?
the only example in the migration guide use spacekey as an example… Is there any KeyCode type (like an Enum) that I can use?
How can I pass “Space” and “JoystickButton0” and get true if they are pressed?
Should I make a custom Enum? Is there a way to do it fast?
And how can I bind that to something like
((KeyControl)Keyboard.current[“space”]).isPressed
?
I have an idea… but I need to convert “Space” to “space” (and know how everything should be converted) and also I need to know how to do a Keyboard.current[“JoystickButton0”] without using Joystick.current (why there is no generic current input to get every input?).
- A lot more problems for the axis, right now I mapped multiple joystick for standard joystick
private static Dictionary<string, string> joypad = new Dictionary<string, string>();
private string[] inputAxisList = { "JoyVertical", "JoyHorizontal", "JoyVertical2", "JoyHorizontal2",
"XMouse", "YMouse", "ScrollWheel" };
SetJoypad(inputAxisList[j], loadConfirmA[j]);
//on android I do:
SetJoypad("JoyVertical", "VerticalJoy3Android");
SetJoypad("JoyHorizontal", "HorizontalJoy3Android");
//on pc I do:
SetJoypad("JoyVertical", "VerticalJoy");
SetJoypad("JoyHorizontal", "HorizontalJoy");
//VerticalJoy, HorizontalJoy, VerticalJoy3Android, HorizontalJoy3Android are mapped on the old Input Mapper
with
GetJoypad(“JoyVertical”) I get the current vertical joypad mapped.
after that I just pass it to
public static float GetAxis(string axis) {
return Input.GetAxis(axis);
}
to get x and y value of that axis
again the migration manual does not help at all
I’m ok with making a new mapping Unity side but why
public InputAction fireAction;
void Awake()
{
fireAction.performed += ctx => Fire();
}
void Fire()
{
//…
}
I don’t really understand how to translate that in my own code. Should I make a List of InputAction? And also a list of action performed… and how can I know if axis x or y return a value?
UnityEngine.Input.mousePosition
Use Mouse.current.position.ReadValue().
NOTE: Mouse simulation from touch is not implemented yet.
so… how can I get the touch position too? I need both since my game is both desktop and mobile.
- Finally my code to rebind
foreach (KeyCode kcode in Enum.GetValues(typeof(KeyCode))) {
if (InputMapperSystem.GetKey(kcode)) {
if (!inputs.ContainsValue(kcode)) {
SetInput(singleInput, kcode);
List<string> temp = new List<string>();
foreach (KeyCode key in inputs.Values) {
temp.Add(key.ToString());
}
//save to json
}
singleInput = "";
takeInput = false;
break;
}
}
I undestand that the new Input system is just different, but I don’t really want to change my code too much.
Is there any way around this kind of things?
I don’t really want my code to became obsolete and I know they will remove the old input system sooner or later… But how can I fix my code without make the input system from zero?