Skip to main content

Switch

This component is a special kind of toggle element. It has a "switch" style visual and interaction. It can be set to on or off and it has it's own specific toggle group called SwitchToggleGroup.

Each switch has two distinct pieces: the knob (or the thumb), and the track.

Component Breakdown



Render OrderSets the renderOrder for the visuals of the element.
SizeSets the size of the element. X is width, Y is height and Z is the depth of the underlying collider.
InactiveMakes the element impossible to interact with. Think "grayed out".
Play AudioTurns on the built-in audio cues for the element.
Has Track VisualTurns on the default track visual for this element.
Custom Knob SizeTurns on controls for the knob size.
Knob Size(shows if Custom Knob Size is checked) Set the size of the knob inside of the element.
Add CallbacksAdds the editor hooks for the event callbacks to the element.
On Value Changed CallbacksA callback for when the element's value changes.
On Finished CallbacksA callback for when the element interaction and changes are all finished.
Default Value(shows if Toggleable is checked) Sets the default state of the toggleable button to "on".

Code Example

import { Switch } from 'SpectaclesUIKit.lspkg/Scripts/Components/Switch/Switch';

/**
* A simple programmatic switch example
*/

@component
export class ExampleSwitchScript extends BaseScriptComponent {
onAwake() {
const demoSwitch = this.sceneObject.createComponent(Switch.getTypeName());
demoSwitch.size = new vec3(10, 4, 1);
demoSwitch.initialize();

demoSwitch.onValueChange.add((value) => {
print(`Switch value changed to ${value}`);
});
}
}
Was this page helpful?
Yes
No