Files
chameleon/Assets/Scripts/Components/UI/SliderDoubleClickReset.cs
T

42 lines
1.8 KiB
C#

using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
/// <summary>
/// Double-clicking a slider resets it to its default value. The default is the slider's authored
/// value, serialized when the component is added (runtime seeding from settings/persistence must not
/// overwrite it, so it is captured at edit time, not in Awake). The reset goes through slider.value,
/// so onValueChanged listeners (persistence, SliderCurve, value labels) fire as if the user dragged.
/// </summary>
[RequireComponent(typeof(Slider))]
public class SliderDoubleClickReset : MonoBehaviour, IPointerClickHandler
{
[SerializeField, Tooltip("Raw slider value restored on double-click. Seeded from the slider's authored value when the component is added.")]
private float _defaultValue;
[SerializeField, Tooltip("Max seconds between the two clicks.")]
private float _maxClickInterval = 0.25f;
[SerializeField, Tooltip("Max pointer movement (screen pixels) between the two clicks.")]
private float _maxPointerDistance = 8f;
private float _lastClickTime = float.NegativeInfinity;
private Vector2 _lastClickPosition;
public void OnPointerClick(PointerEventData eventData)
{
if (eventData.button != PointerEventData.InputButton.Left) return;
// Unitys clickCount (festes ~0.3s-Fenster, kein Positions-Check) bewusst ignoriert.
bool isDouble = Time.unscaledTime - _lastClickTime <= _maxClickInterval
&& (eventData.position - _lastClickPosition).sqrMagnitude <= _maxPointerDistance * _maxPointerDistance;
_lastClickTime = isDouble ? float.NegativeInfinity : Time.unscaledTime;
_lastClickPosition = eventData.position;
if (isDouble) GetComponent<Slider>().value = _defaultValue;
}
private void Reset() => _defaultValue = GetComponent<Slider>().value;
}