docs.unity3d.com
Search Results for

    Show / Hide Table of Contents

    Class FocusExitEventArgs

    Event data associated with the event when an Interaction group ends focusing an Interactable.

    Inheritance
    object
    BaseInteractionEventArgs
    FocusExitEventArgs
    Inherited Members
    BaseInteractionEventArgs.interactorObject
    object.Equals(object)
    object.Equals(object, object)
    object.GetHashCode()
    object.GetType()
    object.MemberwiseClone()
    object.ReferenceEquals(object, object)
    object.ToString()
    Namespace: UnityEngine.XR.Interaction.Toolkit
    Assembly: Unity.XR.Interaction.Toolkit.dll
    Syntax
    public class FocusExitEventArgs : BaseInteractionEventArgs
    Remarks

    When an XRInteractionGroup ends focusing an IXRFocusInteractable, a FocusExitEvent is invoked with FocusExitEventArgs as parameters. The FocusExitEventArgs provides the IXRInteractor object, the IXRFocusInteractable object, the XRInteractionGroup, and the XRInteractionManager object associated with the focus event. It also provides the isCanceled boolean which will be true if the interactor is destroyed or the interactableObject is unregistered with the XRInteractionManager, disabled, or destroyed during focus.

    Examples

    The following example adds a listener to the FocusExitEvent of an XRBaseInteractable and implements custom callback functions that will be called when the FocusExitEvent is invoked.

    using UnityEngine;
    using UnityEngine.XR.Interaction.Toolkit;
    using UnityEngine.XR.Interaction.Toolkit.Interactors;
    using UnityEngine.XR.Interaction.Toolkit.Interactables;
    

    /// <summary> /// This class is an example script that highlights the various XR Interaction events and demonstrates how to create callbacks for each event. /// </summary> class XRInteractionEventsSample : MonoBehaviour { // The interactor to listen to hover and select interaction events. Connect an XRBaseInteractor like NearFarInteractor or XRRayInteractor to this // property in the Editor. [SerializeField] XRBaseInteractor m_ExampleInteractor;

    // The interactable to listen to focus and activate events. Connect XRBaseInteractable like XRGrabInteractable to this property in the Editor
    [SerializeField]
    XRBaseInteractable m_ExampleInteractable;
    
    void OnEnable()
    {
        if (m_ExampleInteractor != null)
        {
            // Add listener to hover events
            m_ExampleInteractor.hoverEntered.AddListener(OnInteractorHoverEntered);
            m_ExampleInteractor.hoverExited.AddListener(OnInteractorHoverExited);
    
            // Add listener to select events
            m_ExampleInteractor.selectEntered.AddListener(OnInteractorSelectEntered);
            m_ExampleInteractor.selectExited.AddListener(OnInteractorSelectExited);
        }
        else
        {
            Debug.LogWarning("Example interactor is null. No hover or select event callbacks will be executed in this example. Ensure the ExampleInteractor field has a " + typeof(XRBaseInteractor) + " assigned.", this);
        }
    
        if (m_ExampleInteractable != null)
        {
            // Add listener to focus events
            m_ExampleInteractable.focusEntered.AddListener(OnInteractableFocusEntered);
            m_ExampleInteractable.focusExited.AddListener(OnInteractableFocusExited);
    
            // Add listener to activate events
            m_ExampleInteractable.activated.AddListener(OnInteractableActivated);
            m_ExampleInteractable.deactivated.AddListener(OnInteractableDeactivated);
        }
        else
        {
            Debug.LogWarning("Example interactable is null. No focus or activate event callbacks will be executed in this example. Ensure the ExampleInteractable field has a " + typeof(XRBaseInteractable) + " assigned.", this);
        }
    }
    
    void OnDisable()
    {
        if (m_ExampleInteractor != null)
        {
            // Remove listener to hover events
            m_ExampleInteractor.hoverEntered.RemoveListener(OnInteractorHoverEntered);
            m_ExampleInteractor.hoverExited.RemoveListener(OnInteractorHoverExited);
    
            // Remove listener to select events
            m_ExampleInteractor.selectEntered.RemoveListener(OnInteractorSelectEntered);
            m_ExampleInteractor.selectExited.RemoveListener(OnInteractorSelectExited);
        }
    
        if (m_ExampleInteractable != null)
        {
            // Remove listener to focus events
            m_ExampleInteractable.focusEntered.RemoveListener(OnInteractableFocusEntered);
            m_ExampleInteractable.focusExited.RemoveListener(OnInteractableFocusExited);
    
            // Remove listener to activate events
            m_ExampleInteractable.activated.RemoveListener(OnInteractableActivated);
            m_ExampleInteractable.deactivated.RemoveListener(OnInteractableDeactivated);
        }
    }
    
    void OnInteractorHoverEntered(HoverEnterEventArgs args)
    {
        // Example of casting args objects to interactor and interactable base classes
        var interactor = args.interactorObject as XRBaseInteractor;
        var interactable = args.interactableObject as XRBaseInteractable;
    
        // Logic for hover enter event goes here
        Debug.Log(args.interactorObject.transform.name + " began hovering " + args.interactableObject.transform.name, this);
    }
    
    void OnInteractorHoverExited(HoverExitEventArgs args)
    {
        // Example of ignoring event when the hover was canceled, such as due to the object being unregistered while hovering
        if (args.isCanceled)
            return;
    
        // Example of casting args objects to interactor and interactable base classes
        var interactor = args.interactorObject as XRBaseInteractor;
        var interactable = args.interactableObject as XRBaseInteractable;
    
        // Logic for hover exit event goes here
        Debug.Log(args.interactorObject.transform.name + " stopped hovering " + args.interactableObject.transform.name, this);
    }
    
    void OnInteractorSelectEntered(SelectEnterEventArgs args)
    {
        // Example of casting args objects to interactor and interactable base classes
        var interactor = args.interactorObject as XRBaseInteractor;
        var interactable = args.interactableObject as XRBaseInteractable;
    
        // Logic for select enter event goes here
        Debug.Log(args.interactorObject.transform.name + " began selecting " + args.interactableObject.transform.name, this);
    }
    
    void OnInteractorSelectExited(SelectExitEventArgs args)
    {
        // Example of ignoring event when the select was canceled, such as due to the object being unregistered while selected
        if (args.isCanceled)
            return;
    
        // Example of casting args objects to interactor and interactable base classes
        var interactor = args.interactorObject as XRBaseInteractor;
        var interactable = args.interactableObject as XRBaseInteractable;
    
        // Logic for select exit event goes here
        Debug.Log(args.interactorObject.transform.name + " stopped selecting " + args.interactableObject.transform.name, this);
    }
    
    void OnInteractableFocusEntered(FocusEnterEventArgs args)
    {
        // Example of casting args objects to interactor and interactable base classes
        var interactor = args.interactorObject as XRBaseInteractor;
        var interactable = args.interactableObject as XRBaseInteractable;
    
        // Logic for focus enter event goes here
        Debug.Log(args.interactorObject.transform.name + " began focusing " + args.interactableObject.transform.name, this);
    }
    
    void OnInteractableFocusExited(FocusExitEventArgs args)
    {
        // Example of casting args objects to interactor and interactable base classes
        var interactor = args.interactorObject as XRBaseInteractor;
        var interactable = args.interactableObject as XRBaseInteractable;
    
        // Logic for focus exit event goes here
        Debug.Log(args.interactorObject.transform.name + " stopped focusing " + args.interactableObject.transform.name, this);
    }
    
    void OnInteractableActivated(ActivateEventArgs args)
    {
        // Example of casting args objects to interactor and interactable base classes
        var interactor = args.interactorObject as XRBaseInteractor;
        var interactable = args.interactableObject as XRBaseInteractable;
    
        // Logic for activate event goes here
        Debug.Log(args.interactorObject.transform.name + " is activating " + args.interactableObject.transform.name, this);
    }
    
    void OnInteractableDeactivated(DeactivateEventArgs args)
    {
        // Example of casting args objects to interactor and interactable base classes
        var interactor = args.interactorObject as XRBaseInteractor;
        var interactable = args.interactableObject as XRBaseInteractable;
    
        // Logic for deactivate event goes here
        Debug.Log(args.interactorObject.transform.name + " is deactivating " + args.interactableObject.transform.name, this);
    }
    

    }

    Properties

    interactableObject

    The Interactable associated with the interaction event.

    Declaration
    public IXRFocusInteractable interactableObject { get; set; }
    Property Value
    Type Description
    IXRFocusInteractable
    See Also
    FocusEnterEvent
    InteractableFocusMode
    IXRInteractor
    IXRFocusInteractable
    XRInteractionManager
    XRBaseInteractable

    interactionGroup

    The interaction group associated with the interaction event.

    Declaration
    public IXRInteractionGroup interactionGroup { get; set; }
    Property Value
    Type Description
    IXRInteractionGroup
    See Also
    FocusEnterEvent
    InteractableFocusMode
    IXRInteractor
    IXRFocusInteractable
    XRInteractionManager
    XRBaseInteractable

    isCanceled

    Whether the focus was lost due to being canceled, such as from either the Interaction group or Interactable being unregistered due to being disabled or destroyed.

    Declaration
    public bool isCanceled { get; set; }
    Property Value
    Type Description
    bool
    See Also
    FocusEnterEvent
    InteractableFocusMode
    IXRInteractor
    IXRFocusInteractable
    XRInteractionManager
    XRBaseInteractable

    manager

    The Interaction Manager associated with the interaction event.

    Declaration
    public XRInteractionManager manager { get; set; }
    Property Value
    Type Description
    XRInteractionManager
    See Also
    FocusEnterEvent
    InteractableFocusMode
    IXRInteractor
    IXRFocusInteractable
    XRInteractionManager
    XRBaseInteractable

    See Also

    FocusEnterEvent
    InteractableFocusMode
    IXRInteractor
    IXRFocusInteractable
    XRInteractionManager
    XRBaseInteractable
    In This Article
    Back to top
    Copyright © 2025 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)