Logo
UI Elements

Switch

A toggle style control element

Overview

Switch extends Interactive and provides a clean on or off toggle with animated handle movement, pivot shifting, and visual state fades.

This component is ideal for settings menus, toggles, or any UI scenario that needs a modern switch control.

Preview


Customization

Visual States

Two groups define how the on or off look appears:

  • Off CG: CanvasGroups active when switch is off
  • On CG: CanvasGroups active when switch is on

Transition Effects

Interactive components support state based transitions through the Styler Object system. This includes color changes on hover, click, and other interactions. For more information, see the Styler page.

Interactive Effects

As a child of Interactive, Switch supports:

  • Interaction states
  • Ripple and trail effects
  • Audio styling
  • Highlight, pressed, and selected transitions

For more information, see the Interactive page.


Properties

Settings

NameTypeDescription
isOnboolCurrent on or off state
invokeAtStartboolInvokes events on Start
handleDurationfloatAnimation duration for handle movement
handleCurveAnimationCurveCurve used for the handle animation

References

NameTypeDescription
switchHandleRectTransformHandle that slides left or right
offCGCanvasGroup[]CanvasGroups used when switch is off
onCGCanvasGroup[]CanvasGroups used when switch is on

Visual Effects

NameTypeDescription
enableRippleboolEnables ripple feedback on pointer down or submit
rippleParentRectTransformParent used when spawning ripple instances
ripplePresetRipple.PresetPreset that defines ripple shape, color, size, and timing
enableTrailboolEnables pointer trail while hovering the button
trailParentRectTransformParent used when spawning trail handlers
trailPresetPointerTrail.PresetPreset that defines trail shape, color, scale, and fade settings

Sound Effects

NameTypeDescription
sfxSourceStylingSourceDefines how audio clips are resolved (for example from Styler presets)
stylerPresetStylerPresetPreset used by Styler to fetch audio and styling data
highlightedSFXAudioMappingAudio mapping used when the button becomes highlighted or hovered
pressedSFXAudioMappingAudio mapping used when the button is pressed or submitted
selectedSFXAudioMappingAudio mapping used when entering the Selected state

Events

NameTypeDescription
onValueChangedUnityEvent<bool>Fired when the value changes
onSwitchOnUnityEventFired when turning on
onSwitchOffUnityEventFired when turning off

Public Methods

NameParametersDescription
SetValue(value)boolSets on or off
SetValue(value, sendCallback)bool, boolSets value and optionally skips events
Toggle()NoneFlips the switch
SetState(state, instant)InteractionState, boolSets Interactive visual state
SetInteractable(value)boolEnables or disables interaction

Code Example

SwitchExample.cs
using UnityEngine;
using Evo.UI;

public class SwitchExample : MonoBehaviour
{
    public Switch mySwitch;

    void Start()
    {
        // Initial state
        mySwitch.SetValue(true);

        // Listen to value changes
        mySwitch.onValueChanged.AddListener(val =>
        {
            Debug.Log("Switch value: " + val);
        });

        mySwitch.onSwitchOn.AddListener(() =>
        {
            Debug.Log("Switch turned ON");
        });

        mySwitch.onSwitchOff.AddListener(() =>
        {
            Debug.Log("Switch turned OFF");
        });

        // Toggle value
        mySwitch.Toggle();

        // Interactive features
        mySwitch.enableRipple = true;
        mySwitch.enableTrail = true;

        mySwitch.SetState(InteractionState.Normal);
        mySwitch.SetInteractable(true);
    }
}

On this page