Search Unity

Question Action reference and composite binding

Discussion in 'Input System' started by downstroypvp, Jul 22, 2022.

  1. downstroypvp

    downstroypvp

    Joined:
    May 30, 2016
    Posts:
    40
    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:

    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):
    1. Separate my action into four actions (PrimaryDirectionUp, PrimaryDirectionDown ...): this is not very convenient as I have polling elsewhere that readValue<Vector2> 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.
    2. 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!
     
  2. Cinnax

    Cinnax

    Joined:
    Dec 16, 2012
    Posts:
    3
    I am not exactly sure what it is that you are trying to do.

    However, you can just directly check the bindings.

    Code (csharp):
    1.  
    2. if (inputAction.bindings[0].isComposite)
    3. {
    4.      string spriteNameUp = String.Format("{0}{1}", inputAction.name, inputAction.bindings[1].name);
    5.      string spriteNameDown = String.Format("{0}{1}", inputAction.name, inputAction.bindings[2].name);
    6.      string spriteNameLeft = String.Format("{0}{1}", inputAction.name, inputAction.bindings[3].name);
    7.      string spriteNameRight = String.Format("{0}{1}", inputAction.name, inputAction.bindings[4].name);
    8. }
    9. else
    10. {
    11.      spriteName = inputAction.name
    12. }
     
    Last edited: Jul 26, 2022
  3. downstroypvp

    downstroypvp

    Joined:
    May 30, 2016
    Posts:
    40
    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:

    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):

    upload_2022-7-29_15-43-46.png 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.
     
  4. downstroypvp

    downstroypvp

    Joined:
    May 30, 2016
    Posts:
    40
    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)