Files
ImaageQ_Unity_App/Assets/Common/Scripts/SliderToggle.cs
Ignacio Gómez Puga 5847d844a5 Removed TOTU 103
2025-03-04 12:04:52 -06:00

38 lines
880 B
C#

// Copyright 2022-2024 Niantic.
using System;
using UnityEngine;
using UnityEngine.UI;
public class SliderToggle : Toggle
{
[SerializeField] private RectTransform uiHandleRectTransform;
private Vector2 _handlePosition;
protected override void Awake()
{
_handlePosition = uiHandleRectTransform.anchoredPosition;
onValueChanged.AddListener(OnSwitch);
if (isOn)
{
OnSwitch(true);
}
base.Awake();
}
private void OnSwitch(bool on)
{
var Xvalue = on ? Math.Abs(_handlePosition.x) : Math.Abs(_handlePosition.x) * -1;
_handlePosition = new Vector2(Xvalue, _handlePosition.y);
uiHandleRectTransform.anchoredPosition = _handlePosition;
}
protected override void OnDestroy()
{
onValueChanged.RemoveListener(OnSwitch);
base.OnDestroy();
}
}