A pointer-up after a drag no longer counts as a click (dragging flag + press-to-release distance within the existing tolerance), so drag -> release -> click cannot trigger a reset anymore. PaintHud prefab: pointer tolerance 4 px, HalfLambert default back to 0.5. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
47 lines
2.0 KiB
C#
47 lines
2.0 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;
|
|
|
|
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<Slider>().value = _defaultValue;
|
|
}
|
|
|
|
private void Reset() => _defaultValue = GetComponent<Slider>().value;
|
|
}
|