Action reference and composite binding

Hi,

I am trying to have a monobehavior that dynamically shows the right sprite for the current InputDevice, according to a Serialized ActionReference.

The problem I am running into is that the action (PrimaryDirection) has a vector2 ControlType and its bindings are composite:

8302671--1088985--upload_2022-7-22_16-51-12.png

I need to reference in the editor this input action's composite part individually (to display the right sprite (eg: leftStick/up). But the InputActionReference only allow me to reference the InputAction, and not a specific binding inside the composite binding.

I have two solution (both ugly imho):

  • Separate my action into four actions (PrimaryDirectionUp, PrimaryDirectionDown ...): this is not very convenient as I have polling elsewhere that readValue from the action. Plus it means that we can't use any composite bindings in our project because we would be unable to retrieve the sprite for it.
  • Store the compositePart as another property inside this monobehavior. This is not really nice either as I would have to fiddle with string concatenation for retrieving the right sprite from my spritesheet (namely concatenate action's binding and this string), plus this field would only be used when the action bindings are composite, and thus I would need to have a "none" value, which is not really self-explanatory.

Is there a more unity way to do it?

Thank you!

I am not exactly sure what it is that you are trying to do.

However, you can just directly check the bindings.

if (inputAction.bindings[0].isComposite)
{
     string spriteNameUp = String.Format("{0}{1}", inputAction.name, inputAction.bindings[1].name);
     string spriteNameDown = String.Format("{0}{1}", inputAction.name, inputAction.bindings[2].name);
     string spriteNameLeft = String.Format("{0}{1}", inputAction.name, inputAction.bindings[3].name);
     string spriteNameRight = String.Format("{0}{1}", inputAction.name, inputAction.bindings[4].name);
}
else
{
     spriteName = inputAction.name
}

Hi and thank you for the answer.

To better explain what I am trying to do here is an image of the behavior I am using right now:

8321574--1092543--upload_2022-7-29_15-38-18.png

Basically, I have an InputActionSprite that reference "something" (for now it references an InputActionReference), and that automatically lookup for the corresponding sprite to display in the UI, depending on the current InputDevice. It works well for actions that doesn't have composite binding, as I can just do a mapping between the action and the sprite to display.

The problem I face is for actions that have a composite bindings, I need to show thoses sprites (the sprite is updated depending on the current InputDevice) (eg: PrimaryDirection/left):

8321574--1092546--upload_2022-7-29_15-43-46.png 8321574--1092549--upload_2022-7-29_15-44-43.png ....

But I haven't found a way to reference the PrimaryDirection/Left/Right... from a serializeProperty

For now I am using a weird direction property to know which of the bindings I want to display the sprite of, but it is not at all generic.

I was wondering if it was possible to reference directly the PrimaryDirection/Left/Right/Up/down so that I don't have to add this field.

As I understand it it looks like it is not possible, but maybe I am missing something.

I have found a way to do it. Here is the Unity example that handles it for the rebind (also contains a snippet for displaying sprite)