using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
///
/// 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.
///
[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;
if (eventData.dragging || (eventData.position - eventData.pressPosition).sqrMagnitude > _maxPointerDistance * _maxPointerDistance)
{
_lastClickTime = float.NegativeInfinity;
return;
}
bool isDouble = Time.unscaledTime - _lastClickTime <= _maxClickInterval
&& (eventData.position - _lastClickPosition).sqrMagnitude <= _maxPointerDistance * _maxPointerDistance;
_lastClickTime = isDouble ? float.NegativeInfinity : Time.unscaledTime;
_lastClickPosition = eventData.position;
if (isDouble) GetComponent().value = _defaultValue;
}
private void Reset() => _defaultValue = GetComponent().value;
}