I have two implementations for showing the correct ui elements based on the currently used controller. The first works as intended, but the second method is incorrect. I’m wondering if there is a more efficient way of doing what i’d like, would appreciate some input!
Working method
Below, Type is a reference to the inputactionreference that’s attached to the label, example ( the join action ). I have two control types, keyboard and gamepad. I loop through the control schemes to find the gamepad and keyboards, and then use that i variable for to show the correct binding and path.
public InputActionReference[] inputActionReference;
for (int i = 0; i < type.asset.controlSchemes.Count; i++)
{
if(type.asset.controlSchemes[i].bindingGroup.Contains("Gamepad"))
label.lblGamepadTxt.text = "<sprite name=" + InputControlPath.ToHumanReadableString(type.action.bindings[i].path, InputControlPath.HumanReadableStringOptions.OmitDevice) + ">";
else if(type.asset.controlSchemes[i].bindingGroup.Contains("Keyboard"))
label.lblKeyboardTxt.text = InputControlPath.ToHumanReadableString(type.action.bindings[i].path, InputControlPath.HumanReadableStringOptions.OmitDevice).ToUpper();
}
Non-Working method ( shows gamepad instead of keyboard )
Below, I get the current control scheme index and pass that into the bindings array, but i find it gives me the wrong scheme. Thoughts?
public InputActionReference inputActionReference;
if (myPlayer.GetComponentInParent<PlayerInput>().currentControlScheme.Contains("Gamepad"))
{
joinObj.GetComponentInChildren<TextMeshPro>().text =
"<sprite name=" + InputControlPath.ToHumanReadableString(inputActionReference.action.bindings[getCurrentControlSchemeIndex()].path,
InputControlPath.HumanReadableStringOptions.OmitDevice) + ">";
}
else if (myPlayer.GetComponentInParent<PlayerInput>().currentControlScheme.Contains("Keyboard"))
{
joinObj.GetComponentInChildren<TextMeshPro>().text =
InputControlPath.ToHumanReadableString(inputActionReference.action.bindings[getCurrentControlSchemeIndex()].path,
InputControlPath.HumanReadableStringOptions.OmitDevice);
}
int getCurrentControlSchemeIndex()
{
int index = inputActionReference.asset.FindControlSchemeIndex(myPlayer.GetComponentInParent<PlayerInput>().currentControlScheme);
return index;
}