I need to use a Unity slider to change the time of an AudioSource. As now the slider is capable to display the time position the AudioSource is on, but I also need it to edit the position. Here's my code:
public Slider time;
public AudioSource audio;
// Start is called before the first frame update
void Start()
{
audio = GetComponent<AudioSource>();
}
// Update is called once per frame
void Update()
{
time.maxValue = audio.clip.length;
time.value = audio.time;
audio.time = time.value;
}
How can I do that?
CodePudding user response:
public Slider slider;
private AudioSource audioS;
private void Awake()
{
audioS = GetComponent<AudioSource>();
}
void Update()
{
slider.maxValue = audioS.clip.length;
}
public void OnSliderValueChanged()
{
audioS.time = slider.value;
}
private void FixedUpdate()
{
slider.value = audioS.time;
}
Once you have this code in your file, simply go to your slider. All the way down in the properties panel, you will find "OnValueChanged" put your object with your script attached to it and simply choose your function. Your function will now be called everytime you change the value of the slider.