Skip to content

basebutton

eisclimber edited this page Apr 4, 2026 · 1 revision

BaseButton

Class in ExPresSXR.Interaction

Inherits from UnityEngine.XR.Interaction.Toolkit.Interactables.XRBaseInteractable

{% hint style="warning" %} This class is obsolete and may be removed from a future version of Yarn Spinner: The button functionality was reimplemented as ValueRangeInteractable. Use an ExPresSXR.Interaction.Button instead. {% endhint %}

Summary

Implements a basic 3D button that can be physically pressed in VR. It may be used as base class to extend on the behavior.

The button can be disabled and used as toggle by setting the respective booleans to true.
Also the button press can be augmented with sounds for pressing and releasing the buttons.

It features two anchors for the base and the push anchor:

  • The base anchor stays in place as it acts as the base of the button.
  • The push anchor will move when being pushed, so anything that should move when being pushed can be a child of this, e.g. the button cap.

It also hold a collider which is used to determine if the button should be pressed which can be customized to fit the objects in the anchor by changing colliderSize.

There are a multitude of Events to listen to including for being pressed, released, reset and having inputDisabled. Note that there are separate Events for toggleMode and normalMode, that are emitted exclusively in these modes. Meaning OnPressed will NOT be triggered if toggleMode = true but instead OnTogglePressed.

A set of buttons can be instantiated via the context menu. To create a custom button it is recommended starting with the empty button prefab.

In order to change color, a ColorSwitcher-Component can be added to components of the push anchor and be connected with the OnPressed and OnReleased (and/or OnTogglePressed and OnToggleReleased) signals to change colors. For reference have a look on the instantiable button prefabs (except the empty one).

For testing button pressed in the editor the ContextMenu of the Button has options to emit the pressed events manually.

public class BaseButton : XRBaseInteractable

Fields

Name Description
BaseAnchor The GameObject that acts as a parent for all static non-moving parts of the button (e.g. the base).
OnButtonPressReset Emitted when the button press is reset.
OnInputDisabled Emitted when input gets disabled.
OnInputEnabled Emitted when input gets disabled.
OnPressed Emitted when the button is pressed in normal mode.
OnReleased Emitted when the button is released in normal mode.
OnTogglePressed Emitted when the button is pressed in toggle mode.
OnToggleReleased Emitted when the button is released in toggle mode.
PressedSound Sound played when the button is NOT in toggle mode and pressed down.
PushAnchor The GameObject that acts as a parent for all moving parts of the button (e.g. the button cap).
ReleasedSound Sound played when the button is NOT in toggle mode and is released.
ToggledDownSound Sound played when the button is in toggle mode and is toggled from the up to the down position.
ToggledUpSound Sound played when the button is in toggle mode and is toggled from the down to the up position.

Methods

Name Description
InternalEmitInputDisabledEvents() For internal use only. Re-emits the input disabled signals.
IsHoverableBy(IXRHoverInteractor) Determines if the Button can be hovered or in this case pressed by an IXRHoverInteractor.
PlayPressedSound() Plays the pressedSound, if assigned.
PlayReleasedSound() Plays the releasedSound, if assigned.
PlayToggledDownSound() Plays the toggledDownSound, if assigned.
PlayToggledUpSound() Plays the toggledUpSound, if assigned.
ProcessInteractable(XRInteractionUpdateOrder.UpdatePhase) Processes the interactor to detect button interactions.
Can be overwritten further, but requires a base.ProcessInteractable(updatePhase); call at the beginning.
ResetButtonPress() Allows to reset the button press moving it in up-position.

Properties

Name Description
AllowNearFarInteraction If enabled allows NearFarInteractors to be treates as valid Direct Interactor.
It is recommended to set the max interaction distance to the size of near interaction volume,
as we can not differentiate hovers from it and the ray.
ColliderSize The Size of the box collider component of the pushAnchor that determines the area in which presses are detected. It should wrap around the objects in the pushAnchor.
A common source of error is if set to (0, 0, 0) no pressed will be detected.
InputDisabled If enabled, the button will refuse input and will stay in the up-position.
MaxInteractionDistance Max distance to to an interactor to be able to interact with the button.
This is a hack for being able to determine if the button is hovered near or far.
Pressed If the button is currently considered pressed (or toggled down).
ToggleMode If enabled, the button will be in toggle mode and stay in the up or down position after being pressed.

ExPresS XR Wiki

Tutorial Pages

Code Documentation

Clone this wiki locally