Table of Contents

Class ToggleBase

Namespace
Fallencake.UI
Assembly
Fallencake.UI.dll

Base toggle control that encapsulates toggle state, events and saving logic. Inherits from MultipleGraphicsSelectable to reuse transition and target graphics system.

public abstract class ToggleBase : MultipleGraphicsSelectable, IMultiSelectable, ISaveable
Inheritance
object
Object
Component
Behaviour
MonoBehaviour
UIBehaviour
Selectable
ToggleBase
Implements
Derived
Inherited Members

Fields

OnValueChanged

Invoked when the toggle value changes or when alwaysNotify is requested.

public SwitchEvent OnValueChanged

Field Value

SwitchEvent

_ease

[SerializeField]
protected AnimationCurve _ease

Field Value

AnimationCurve

_group

[SerializeField]
protected ModularToggleGroup _group

Field Value

ModularToggleGroup

_handle

[SerializeField]
protected RectTransform _handle

Field Value

RectTransform

_invokeEventsOnStart

[SerializeField]
protected bool _invokeEventsOnStart

Field Value

bool

_isInitialized

protected bool _isInitialized

Field Value

bool

_isOn

[SerializeField]
protected bool _isOn

Field Value

bool

_switchDuration

[SerializeField]
[Range(0.1, 10)]
protected float _switchDuration

Field Value

float

Properties

IsOn

Whether the toggle is currently active.

public bool IsOn { get; set; }

Property Value

bool

IsSavingEnabled

Enables save/load behavior for the component.

public bool IsSavingEnabled { get; set; }

Property Value

bool

SaveKey

PlayerPrefs key used to persist the state.

public string SaveKey { get; set; }

Property Value

string

Methods

AnimateSwitch()

Toggle the current value and update visuals/events.

public virtual void AnimateSwitch()

Awake()

Convenience function to get the Animator component on the GameObject.

protected override void Awake()

Examples

using UnityEngine;
using System.Collections;
using UnityEngine.UI; // Required when Using UI elements.

public class ExampleClass : MonoBehaviour
{
    private Animator buttonAnimator;
    public Button button;

    void Start()
    {
        //Assigns the "buttonAnimator" with the button's animator.
        buttonAnimator = button.animator;
    }
}

EditorApplyIsOn()

Apply IsOn from inspector instantly; safe for edit and play modes.

public void EditorApplyIsOn()

Initialize()

protected virtual void Initialize()

InstantSwitch(bool)

Instantly update visuals of handle/background.

protected virtual void InstantSwitch(bool isOn)

Parameters

isOn bool

LoadState()

Loads component state from persistent storage.

public void LoadState()

OnDisable()

protected override void OnDisable()

OnEnable()

protected override void OnEnable()

OnPointerClick(PointerEventData)

Handle pointer click to toggle value when interactable.

public virtual void OnPointerClick(PointerEventData eventData)

Parameters

eventData PointerEventData

OnSubmit(BaseEventData)

Handle submit (e.g., keyboard) to toggle value when interactable.

public virtual void OnSubmit(BaseEventData eventData)

Parameters

eventData BaseEventData

PlaySwitchTween(bool)

Play animated transition between states. Override in derived controls.

protected virtual void PlaySwitchTween(bool isOn)

Parameters

isOn bool

SaveState()

Saves component state to persistent storage.

public void SaveState()

Set(bool, bool, bool, bool)

Core setter with semantics similar to Unity's Toggle.Set.

protected void Set(bool value, bool sendCallback = true, bool animate = true, bool alwaysNotify = false)

Parameters

value bool

Desired toggle value.

sendCallback bool

Whether to invoke OnValueChanged.

animate bool

Whether to animate transition (otherwise updates instantly).

alwaysNotify bool

If true, invokes OnValueChanged even if the value did not change.

SetIsOn(bool, bool, bool, bool)

Sets the toggle value using the core Set(...) semantics. Exposes control over callbacks, animation and forced notification.

public void SetIsOn(bool value, bool sendCallback = true, bool animate = true, bool alwaysNotify = false)

Parameters

value bool

Desired toggle value.

sendCallback bool

Whether to invoke OnValueChanged.

animate bool

Whether to animate transition.

alwaysNotify bool

If true, invokes OnValueChanged even when value did not change.

SetIsOnWithoutNotify(bool, bool)

Sets the toggle value without invoking OnValueChanged. Intended for group-internal updates.

public void SetIsOnWithoutNotify(bool value, bool animate = false)

Parameters

value bool

Desired toggle value.

animate bool

Whether to animate transition.

SetOff()

Set toggle to Off state.

public virtual void SetOff()

SetOn()

Set toggle to On state.

public virtual void SetOn()

SetToggleGroup(ModularToggleGroup, bool)

Assigns toggle to a group, managing registration and initial notification.

protected void SetToggleGroup(ModularToggleGroup newGroup, bool setMemberValue)

Parameters

newGroup ModularToggleGroup
setMemberValue bool

SwitchOff()

Play animated transition to Off state without callback.

public void SwitchOff()

SwitchOn()

Play animated transition to On state without callback.

public void SwitchOn()

SwitchView(bool, bool)

Update visuals either instantly or with tween.

protected virtual void SwitchView(bool isOn, bool isInstant = false)

Parameters

isOn bool
isInstant bool