Skip to main content

Switch

Reference for the UI Kit 4.0.0 Switch component: an on/off toggle switch for binary settings in SPECS Lenses, with System 3.0 styles, track markers, and value events.

  • Snapchat: not supported
  • SPECS: fully supported
  • Camera Kit Web: not supported
  • Camera Kit Mobile: not supported
  • SnapchatNot supported
  • SPECSFully supported
  • Camera Kit WebNot supported
  • Camera Kit MobileNot supported

Platform details

  • Snapchat — Not supported.

  • SPECS — Fully supported.

  • Camera Kit Web — Not supported.

  • Camera Kit Mobile — Not supported.

The UI Kit Switch component is a special kind of toggle element with a "switch" style visual and interaction. It can be set to on or off, and it has its own specific toggle group, SwitchToggleGroup (see Toggle Group).

Each switch has a knob (or thumb) and a track, plus a fill on the track. The Switch is built on the Slider, so it shares the Slider's knob and track settings.

A System 3.0 switch in its on state

Switch Inspector properties

The Switch component in the Inspector, with Theme set to System 3.0 so the Style field shows
PropertyDescription
Render OrderSets the renderOrder for the visuals of the element.
InactiveMakes the element ignore input and shows its inactive look. Think "grayed out."
Play AudioTurns on the built-in audio cues for the element.
ThemeThe theme the switch uses: Inherited (the default, follows the global theme) or System 3.0.
Has Track VisualShows the fill on the track up to the knob. On by default.
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.
Knob IconAn optional icon drawn on the knob.
Add CallbacksAdds the editor hooks for the event callbacks to the element.
On Value Changed Callbacks(shows if Add Callbacks is checked) A callback for when the element's value changes.
SizeSets the size of the element. X is width, Y is height and Z is the depth of the underlying collider. The default is 4.5 × 2.5 × 1.
Style(shows if Theme is System 3.0) Flat (the default), Prism, PrismWide, or PrismSquare.
On Interaction Finished Callbacks(shows if Add Callbacks is checked) A callback for when the element interaction and changes are all finished.
Default ValueThe state the switch starts in: Off (the default) or On.
Left Marker Is TextShows text on the left half of the track instead of an icon.
Left Icon(shows if Left Marker Is Text is unchecked) An optional icon pinned to the left half of the track, above the knob.
Left Text(shows if Left Marker Is Text is checked) A short label pinned to the left half of the track, above the knob.
Right Marker Is TextShows text on the right half of the track instead of an icon.
Right Icon(shows if Right Marker Is Text is unchecked) An optional icon pinned to the right half of the track, above the knob.
Right Text(shows if Right Marker Is Text is checked) A short label pinned to the right half of the track, above the knob.
Track Icon SizeThe width and height, in centimeters, of the left and right track markers; text markers take their font size from it. The default is 1.5.

Create a Switch from a script

The script below creates a switch and prints its value when it changes. onValueChange passes the value as a number: 1 when on, 0 when off. Read the state with isOn, and set it with toggle(on) or isOn. Both fire the value events, so set the initial state before you add your handlers. onFinished fires when a change of state is finished; its argument is true when the user or your code made the change and false when a toggle group did.

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

/**
* A simple programmatic switch example
*/

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

demoSwitch.onValueChange.add((value) => {
print(`Switch value changed to ${value}`);
});
}
}
  • Toggle Group: group switches with a SwitchToggleGroup so only one is on.
  • Slider: the component the Switch is built on.