123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.EventSystems;
- using UnityEngine.UI;
- public class LerpSlider : UnityEngine.UI.Slider, IPointerDownHandler
- {
- private float lerpDuration = 0.08f; // 过渡时间
- private float targetValue;
- private bool isLerping;
- protected override void Update()
- {
- base.Update();
- 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)
- {
- // 计算点击位置的值
- 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之间
- isLerping = true;
- }
- }
|