docs.unity3d.com
Search Results for

    Show / Hide Table of Contents

    Class PressInteraction

    Performs the action at specific points in a button press-and-release sequence according top behavior.

    Inheritance
    object
    PressInteraction
    Implements
    IInputInteraction
    Namespace: UnityEngine.InputSystem.Interactions
    Assembly: Unity.InputSystem.dll
    Syntax
    public class PressInteraction : IInputInteraction
    Remarks

    By default, uses PressOnly which performs the action as soon as the control crosses the button press threshold defined by pressPoint. The action then will not trigger again until the control is first released.

    Can be set to instead trigger on release (that is, when the control goes back below the button press threshold) using ReleaseOnly or can be set to trigger on both press and release using PressAndRelease).

    Note that using an explicit press interaction is only necessary if the goal is to either customize the press behavior of a button or when binding to controls that are not buttons as such (the press interaction compares magnitudes to pressPoint and thus any type of control that can deliver a magnitude can act as a button). The default behavior available out of the box when binding Button type actions to button-type controls (ButtonControl) corresponds to using a press modifier with behavior set to PressOnly and pressPoint left at default.

    Fields

    behavior

    Determines how button presses trigger the action.

    Declaration
    [Tooltip("Determines how button presses trigger the action. By default (PressOnly), the action is performed on press. With ReleaseOnly, the action is performed on release. With PressAndRelease, the action is performed on press and release.")]
    public PressBehavior behavior
    Field Value
    Type Description
    PressBehavior
    Remarks

    By default (PressOnly), the action is performed on press. With ReleaseOnly, the action is performed on release. With PressAndRelease, the action is performed on press and on release.

    pressPoint

    Amount of actuation required before a control is considered pressed.

    Declaration
    [Tooltip("The amount of actuation a control requires before being considered pressed. If not set, default to 'Default Press Point' in the global input settings.")]
    public float pressPoint
    Field Value
    Type Description
    float
    Remarks

    If zero (default), defaults to defaultButtonPressPoint.

    Methods

    Process(ref InputInteractionContext)

    Perform processing of the interaction in response to input.

    Declaration
    public void Process(ref InputInteractionContext context)
    Parameters
    Type Name Description
    InputInteractionContext context
    Remarks

    This method is called whenever a control referenced in the binding that the interaction sits on changes value. The interaction is expected to process the value change and, if applicable, call Started() and/or its related methods to initiate a state change.

    Note that if "control disambiguation" (i.e. the process where if multiple controls are bound to the same action, the system decides which control gets to drive the action at any one point) is in effect -- i.e. when either Button or Value are used but not if PassThrough is used -- inputs that the disambiguation chooses to ignore will cause this method to not be called.

    Note that this method is called on the interaction even when there are multiple interactions and the interaction is not the one currently in control of the action (because another interaction that comes before it in the list had already started the action). Each interaction will get processed independently and the action will decide when to use which interaction to drive the action as a whole.

    // Processing for an interaction that will perform the action only if a control
    // is held at least at 3/4 actuation for at least 1 second.
    public void Process(ref InputInteractionContext context)
    {
    var control = context.control;

             // See if we're currently tracking a control.
             if (m_Control != null)
             {
                 // Ignore any input on a control we're not currently tracking.
                 if (m_Control != control)
                     return;
    
                 // Check if the control is currently actuated past our 3/4 threshold.
                 var isStillActuated = context.ControlIsActuated(0.75f);
    
                 // See for how long the control has been held.
                 var actuationTime = context.time - context.startTime;
    
                 if (!isStillActuated)
                 {
                     // Control is no longer actuated above 3/4 threshold. If it was held
                     // for at least a second, perform the action. Otherwise cancel it.
    
                     if (actuationTime >= 1)
                         context.Performed();
                     else
                         context.Cancelled();
                 }
    
                 // Control changed value somewhere above 3/4 of its actuation. Doesn't
                 // matter to us so no change.
             }
             else
             {
                 // We're not already tracking a control. See if the control that just triggered
                 // is actuated at least 3/4th of its way. If so, start tracking it.
    
                 var isActuated = context.ControlIsActuated(0.75f);
                 if (isActuated)
                 {
                     m_Control = context.control;
                     context.Started();
                 }
             }
         }
    
         InputControl m_Control;
    
         public void Reset()
         {
             m_Control = null;
         }</code></pre></example>
    

    Reset()

    Reset state that the interaction may hold. This should put the interaction back in its original state equivalent to no input yet having been received.

    Declaration
    public void Reset()

    Implements

    IInputInteraction

    Did you find this page useful? Please give it a rating:

    Thanks for rating this page!

    Report a problem on this page

    What kind of problem would you like to report?

    • This page needs code samples
    • Code samples do not work
    • Information is missing
    • Information is incorrect
    • Information is unclear or confusing
    • There is a spelling/grammar error on this page
    • Something else

    Thanks for letting us know! This page has been marked for review based on your feedback.

    If you have time, you can provide more information to help us fix the problem faster.

    Provide more information

    You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:

    You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:

    You've told us there is information missing from this page. Please tell us more about what's missing:

    You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:

    You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:

    You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:

    You've told us this page has a problem. Please tell us more about what's wrong:

    Thank you for helping to make the Unity documentation better!

    Your feedback has been submitted as a ticket for our documentation team to review.

    We are not able to reply to every ticket submitted.

    In This Article
    • Fields
      • behavior
      • pressPoint
    • Methods
      • Process(ref InputInteractionContext)
      • Reset()
    • Implements
    Back to top
    Copyright © 2024 Unity Technologies — Trademarks and terms of use
    • Legal
    • Privacy Policy
    • Cookie Policy
    • Do Not Sell or Share My Personal Information
    • Your Privacy Choices (Cookie Settings)