| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556 | using System;using System.Collections;using System.Collections.Generic;using UnityEngine;using UnityEngine.EventSystems;using UnityEngine.Serialization;using UnityEngine.UI;public class LerpSlider : UnityEngine.UI.Slider, IPointerDownHandler{    private float lerpDuration = 0.08f; // 过渡时间    private float targetValue;    private bool isLerping;    public bool UseLerping=true;    public Action OnPointDown;        protected override void Update()    {        base.Update();        if(!UseLerping)            return;        if (isLerping)        {            // 平滑过渡到目标值            value = Mathf.Lerp(value, targetValue, Time.deltaTime / lerpDuration);            // 检查是否接近目标值,结束过渡            if (Mathf.Abs(value - targetValue) < 0.01f)            {                value = targetValue;                isLerping = false;            }        }    }    public void OnPointerDown(PointerEventData eventData)    {        OnPointDown?.Invoke();        // 计算点击位置的值        RectTransformUtility.ScreenPointToLocalPointInRectangle(            GetComponent<RectTransform>(), eventData.position, eventData.pressEventCamera, out var localPoint);        // 计算滑块的点击位置对应的值        float normalizedValue = Mathf.InverseLerp(            GetComponent<RectTransform>().rect.xMin,            GetComponent<RectTransform>().rect.xMax,            localPoint.x        );        targetValue = Mathf.Clamp01(normalizedValue); // 确保值在0到1之间        UseLerping = true;        isLerping = true;    }}
 |