微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

滑块移动不平稳 Unity3d

如何解决滑块移动不平稳 Unity3d

我有一个音频剪辑和一个滑块。滑块充当音频剪辑的进度条(时间轴)。我可以暂停和播放音频,也可以跳过音轨。一切正常。问题是滑块不能平稳移动,有点抖动。如果有人可以编辑它。预先感谢。

代码如下:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class MusicPlayer : MonoBehavIoUr
{
    AudioSource audioSource;
    Slider slider;
    public AudioClip track;

    // Start is called before the first frame update
    void Start()
    {
        audioSource = GetComponent<AudioSource>();
        slider = GetComponent<Slider>();

        audioSource.clip = track;
        audioSource.Play();

        slider.minValue = 0;
        slider.maxValue = track.length;
    }

    // Update is called once per frame
    void Update()
    {
        slider.value = audioSource.time;
        
    } 

    public void MovePoition()
    {
        audioSource.time = slider.value;
    }

    public void play()
    {
        audioSource.Play();
    }

    public void pause()
    {
        audioSource.Pause();
    }

}

解决方法

Update上,通过使用Time.deltaTime更新滑块(如果正在播放)的值来确保平滑度。否则,以及在playpause中,将位置与播放时间重新同步。

但是,在设置值时,您需要避免回调。在Start创建一个空事件,并在更新值时将其分配给滑块。

最后,添加一个标志来跟踪音轨是否应该播放。这样可以防止在播放剪辑的结尾然后拖动滑块时停止播放源。

MovePoition中设置了源时间之后,根据剪辑的压缩程度,可能无法将其设置为滑块具有的确切值。因此,您应该使用音频源决定使用的时间重新更新滑块值。

一起:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class MusicPlayer : MonoBehaviour
{
    AudioSource audioSource;
    Slider slider;
    Slider.SliderEvent emptyEvent;
    public AudioClip track;
    private bool isPaused;
    private bool needSync;

    // Start is called before the first frame update
    void Start()
    {
        audioSource = GetComponent<AudioSource>();
        slider = GetComponent<Slider>();

        audioSource.clip = track;
        audioSource.Play();

        slider.minValue = 0;
        slider.maxValue = track.length;

        emptyEvent = new Slider.SliderEvent();
    }

    void SetSliderWithoutCallback(float time) 
    {
        Slider.SliderEvent temp = slider.onValueChanged;
        slider.onValueChanged = emptyEvent;
        slider.value = time;
        slider.onValueChanged = temp;
    }

    // Update is called once per frame
    void Update()
    {
        if (audioSource.isPlaying) 
        {
            float newTime = Mathf.Min(slider.value + Time.deltaTime,slider.maxValue);
            SetSliderWithoutCallback(newTime);
        } 
        else if (!isPaused)
        {
            SetSliderWithoutCallback(slider.maxValue);
            isPaused = true;
        }
    } 

    public void MovePoition()
    {
        audioSource.time = slider.value;
        if (!isPaused) 
        {
            audioSource.Play();
            SetSliderWithoutCallback(audioSource.time);
        }
    }

    public void play()
    {
        audioSource.Play();
        isPaused = false;
        SetSliderWithoutCallback(audioSource.time);
    }

    public void pause()
    {
        isPaused = true;
        audioSource.Pause();
        SetSliderWithoutCallback(audioSource.time);
    }

}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。