
~ Unity Wisdom of this site do not provide plain solutions to your problem. Instead, they serve as clues to the puzzle you are trying to solve. ~
[Example Use Case]: You are using Input System. You have one button that triggers weapons, but the interactions of the weapons may vary, for example you have weapons that require holding the button down for different duration, or you may have completely different interaction types like hold and tap.
[Verified with] : Unity 2022.1.0bxx
[Key Method]: InputAction. ApplyBindingOverride ( InputBinding )
[Clue]: You need to find an InputAction, which should be in an InputActionAsset (the asset where you do all the bindings, so you need to store a reference of it). You may need to go through multiple layers in order to find what you want (InputActionAsset->ActionMap->Action). As for the InputBinding part, you will have to reconstruct it by using a string representation of the interaction you want. You can use :
new InputBinding {overrideInteractions = [the_string_representation] }.
The string representation is simple:
" InteractionType ( param1 = xx , param2 = yy , ...)"
For example: An interaction of Tap with duration = 0.5 is represented as:
"Tap(duration=0.5)"
You should be good to solve this problem now.
[Troubleshooting]
1. Things you might need to consider in InputActionAsset:
Each Action can have multiple mappings, like X on the keyboard and A on a controller. Each mapping can have their own interactions. When you override with the above-mentionned method, without supplying things other than overrideInteractions, you will be modifying interactions for ALL mappings of that action. This will not really affect default interactions ( when you edit the InputActionAsset, default interactions are those you add to the Action directly, not individual mappings). So if you have any default interactions left on the action, they will be treated as fallback options. That is, if you have Tap on a specific mapping and you have SlowTap as you default interaction, when Tap is cancelled due to extensive pressing, SlowTap will be evaluated even you did not specify it in the mapping.
2. ToString() of floats
When you construct the string, you may want to convert a float to string. There is a not so famous trap that is involved in this process. If you use the default ToString() options, the Decimal Separator of the float may not be what you are expecting. For those who don't know, there are people in this world that use comma ( , ) as decimal separator, and this is reflected in the ToString() method. If you or your target audience is using for example, a French system, and you are converting 0.5 to string, you will get 0,5 and not 0.5. And Unity will not be happy with that. To fix this issue, you have to explicitly specify .ToString(CultureInfo.InvariantCulture)