Logo
UI Elements

Switch

A toggle with animated on and off states

Overview

Switch represents a boolean setting. It combines an animated handle with Interactive input and optional on/off visual groups.

Preview

Usage

Create a Switch prefab.

Choose its initial state.

Connect On Value Changed to a method accepting a boolean.


Customization

  • Adjust Handle Duration and its curve for the slide animation.
  • The on/off CanvasGroups are the switch’s content states, separate from Interactive’s hover and pressed colors.

Public API

Use Switch in the Evo.UI namespace.

Properties

NameTypeDescription
IsOnboolGets or sets the toggle state.

Fields

NameTypeDescription
handleDurationfloatDuration of the handle transition.

Methods

NameParametersDescription
SetValue()bool valueSets the state with change events enabled.
SetValue()bool value, bool invokeEventsSets the state. Pass false for invokeEvents to suppress events.
Toggle()NoneInverts the current value.

Events

NameTypeDescription
onValueChangedUnityEvent<bool>Invoked with the new boolean value.

Example

SwitchExample.cs
using UnityEngine;
using Evo.UI;

public class SwitchExample : MonoBehaviour
{
    [SerializeField] private Switch target;

    private void Start()
    {
        target.SetValue(true, invokeEvents: false); // Restore the initial setting.
        target.onValueChanged.AddListener(OnMusicChanged);
    }

    public void MuteMusic()
    {
        target.IsOn = false; // Updates the visuals and notifies listeners if changed.
    }

    private void OnMusicChanged(bool enabled)
    {
        Debug.Log($"Music: {enabled}");
    }

    private void OnDestroy()
    {
        if (target != null)
            target.onValueChanged.RemoveListener(OnMusicChanged);
    }
}

On this page