Version: 2023.2+
This example demonstrates how to create a “switch-like” variation of a toggle.
The example creates a custom control: a toggle that users can flip with the mouse, the keyboard, a gamepad, and other devices. It comes with a label that can describe what the toggle represents.
You can find the completed files that this example creates in this GitHub repository.
This guide is for developers familiar with the Unity Editor, UI Toolkit, and C# scripting. Before you start, get familiar with the following:
Create the slide toggle class with a C# script.
slide-toggle
to store your files.slide-toggle
folder, create a C# script named SlideToggle.cs
.SlideToggle.cs
in a text editor and replace its content with the following:Style the slide toggle with a USS file.
slide-toggle
folder, create a USS file named SlideToggle.uss
.SlideToggle.uss
in a text editor and replace its contents with the following:slide-toggle
folder, create a UI Document file named SlideToggleUsage.uxml
.SlideToggleUsage.uxml
in the UI Builder.SlideToggle.uss
as the existing USS.SlideToggleUsage.uxml
into the Source Asset field in the Inspector.