Class Vector2Composite | Input System | 1.0.2
docs.unity3d.com
    Show / Hide Table of Contents

    Class Vector2Composite

    A 2D planar motion vector computed from an up+down button pair and a left+right button pair.

    Inheritance
    Object
    InputBindingComposite
    InputBindingComposite<Vector2>
    Vector2Composite
    Inherited Members
    InputBindingComposite<Vector2>.valueType
    InputBindingComposite<Vector2>.valueSizeInBytes
    InputBindingComposite<Vector2>.ReadValue(InputBindingCompositeContext, Void*, Int32)
    InputBindingComposite<Vector2>.ReadValueAsObject(InputBindingCompositeContext)
    InputBindingComposite.GetExpectedControlLayoutName(String, String)
    Namespace: UnityEngine.InputSystem.Composites
    Syntax
    [Preserve]
    [DisplayStringFormat("{up}/{left}/{down}/{right}")]
    public class Vector2Composite : InputBindingComposite<Vector2>
    Remarks

    This composite allows to grab arbitrary buttons from a device and arrange them in a D-Pad like configuration. Based on button presses, the composite will return a normalized direction vector (normalization can be turned off via UnityEngine.InputSystem.Composites.Vector2Composite.normalize).

    Opposing motions cancel each other out. Meaning that if, for example, both the left and right horizontal button are pressed, the resulting horizontal movement value will be zero.

    Fields

    down

    Binding for the button that down (i.e. (0,-1)) direction of the vector.

    Declaration
    public int down
    Field Value
    Type Description
    Int32
    Remarks

    This property is automatically assigned by the input system.

    left

    Binding for the button that left (i.e. (-1,0)) direction of the vector.

    Declaration
    public int left
    Field Value
    Type Description
    Int32
    Remarks

    This property is automatically assigned by the input system.

    mode

    How to synthesize a Vector2 from the values read from up, down, left, and right.

    Declaration
    public Vector2Composite.Mode mode
    Field Value
    Type Description
    Vector2Composite.Mode

    Determines how X and Y of the resulting Vector2 are formed from input values.

    Remarks
    var action = new InputAction();
    
    // DigitalNormalized composite (the default). Turns gamepad left stick into
    // control equivalent to the D-Pad.
    action.AddCompositeBinding("2DVector(mode=0)")
        .With("up", "<Gamepad>/leftStick/up")
        .With("down", "<Gamepad>/leftStick/down")
        .With("left", "<Gamepad>/leftStick/left")
        .With("right", "<Gamepad>/leftStick/right");
    
    // Digital composite. Turns gamepad left stick into control equivalent
    // to the D-Pad except that diagonals will not be normalized.
    action.AddCompositeBinding("2DVector(mode=1)")
        .With("up", "<Gamepad>/leftStick/up")
        .With("down", "<Gamepad>/leftStick/down")
        .With("left", "<Gamepad>/leftStick/left")
        .With("right", "<Gamepad>/leftStick/right");
    
    // Analog composite. In this case results in setup that behaves exactly
    // the same as leftStick already does. But you could use it, for example,
    // to swap directions by binding "up" to leftStick/down and "down" to
    // leftStick/up.
    action.AddCompositeBinding("2DVector(mode=2)")
        .With("up", "<Gamepad>/leftStick/up")
        .With("down", "<Gamepad>/leftStick/down")
        .With("left", "<Gamepad>/leftStick/left")
        .With("right", "<Gamepad>/leftStick/right");

    right

    Binding for the button that right (i.e. (1,0)) direction of the vector.

    Declaration
    public int right
    Field Value
    Type Description
    Int32
    Remarks

    This property is automatically assigned by the input system.

    up

    Binding for the button that up (i.e. (0,1)) direction of the vector.

    Declaration
    public int up
    Field Value
    Type Description
    Int32
    Remarks

    This property is automatically assigned by the input system.

    Methods

    EvaluateMagnitude(ref InputBindingCompositeContext)

    Determine the current level of actuation of the composite.

    Declaration
    public override float EvaluateMagnitude(ref InputBindingCompositeContext context)
    Parameters
    Type Name Description
    InputBindingCompositeContext context

    Callback context for the binding composite. Use this to access the values supplied by part bindings.

    Returns
    Type Description
    Single
    Overrides
    InputBindingComposite.EvaluateMagnitude(ref InputBindingCompositeContext)
    Remarks

    This method by default returns -1, meaning that the composite does not support magnitudes. You can override the method to add support for magnitudes.

    See EvaluateMagnitude() for details of how magnitudes work.

    See Also
    EvaluateMagnitude()

    ReadValue(ref InputBindingCompositeContext)

    Read a value for the composite given the supplied context.

    Declaration
    public override Vector2 ReadValue(ref InputBindingCompositeContext context)
    Parameters
    Type Name Description
    InputBindingCompositeContext context

    Callback context for the binding composite. Use this to access the values supplied by part bindings.

    Returns
    Type Description
    Vector2

    The current value of the composite according to the state made accessible through context.

    Overrides
    UnityEngine.InputSystem.InputBindingComposite<UnityEngine.Vector2>.ReadValue(UnityEngine.InputSystem.InputBindingCompositeContext)
    Remarks

    This is the main method to implement in custom composites.

    public class CustomComposite : InputBindingComposite<float>
    {
        [InputControl(layout = "Button")]
        public int button;
    
        public float scaleFactor = 1;
    
        public override float ReadValue(ref InputBindingComposite context)
        {
            return context.ReadValue<float>(button) * scaleFactor;
        }
    }

    The other method to consider overriding is EvaluateMagnitude(ref InputBindingCompositeContext).

    See Also
    ReadValue<TValue>()
    ReadValue<TValue>()
    Back to top
    Copyright © 2023 Unity Technologies — Terms of use
    • Legal
    • Privacy Policy
    • Cookies
    • Do Not Sell or Share My Personal Information
    • Your Privacy Choices (Cookie Settings)
    "Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
    Generated by DocFX on 18 October 2023