- Home /
UI Slider to Match Audio Clip Duration
OK there does not seem to be a easy method of getting the New UI sliders to match audio clip duration. How can I code a script in C# that will do the trick? The answer down below is a good start but NOT a solution?? I have tried some variations on the script below but so far no luck getting a slider to maths the duration of the audio file. I'm a little disappointed that this type of thing is not built in.??
Answer by darnoldi · Mar 03, 2015 at 11:20 AM
This should point you in the right direction.
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class AudioSlider : MonoBehaviour {
public Slider audioSlider;
// Use this for initialization
void Start () {
audioSlider.direction = Slider.Direction.LeftToRight;
audioSlider.minValue = 0;
audioSlider.maxValue = audio.clip.length;
audio.Play();
}
// Update is called once per frame
void Update () {
audioSlider.value = audio.time;
}
}
Thanks darnoldi, Well that got my slider moving to the music only it creates one hell of a sound effect lol that could be useful but not right now. What happens is that it seems to cause my audio source to play really fast... at least I think that is what it's doing. I have this script on the main camera that has the audio source. What might I be missing here? This gives part of the answer as in it gets my bar to move but not in the way I want??