Slider that updates based on code unless touched?

I would like to make playheads using the GUI sliders and audio.time, but if that is possible, I can’t figure it out. I need a way to override the value of a slider only if it is being clicked upon. Otherwise, I want it to update based on audio.time/audio.length.

You can, never tried though…

Just create your slider and update it with your audio.time

After than check GUI.changed to know if your user has input some data and override your audio.time with that info

.org

You can’t change the slider if it’s being updated through other code, though, apparently.

Why don’t you check mousedown together with Rect.contains on the slider - if both are true, you write to the slider value based on the mouse offset from the left of the slider, otherwise just make it equal to whatever value you last had plus the elapsed time.

ok i tested… i think this is what you need

using UnityEngine;
using System.Collections;

public class Slider : MonoBehaviour {

	private float mSlidertime;

	// Use this for initialization
	void Start () 
	{
		mSlidertime = 0.0f;
	}
	
	// Update is called once per frame
	void Update () 
	{
		mSlidertime += Time.deltaTime;
	}
	
	
	void OnGUI ()
	{
		mSlidertime = GUI.HorizontalSlider (new Rect (10, 10, 200, 20), mSlidertime, 0.0f, 10.0f);
		
		if ( GUI.changed )
		{
			Debug.Log ("New audio.time=" + mSlidertime);
		}
	}
}

Just modify it to suit your needs

.org

Wow, I was just working on this (using your previous advice), and almost had a functional solution, but yours is less code and works better. Thanks a ton!

This should work as well (js)

var oldTime = audio.time;
var newTime = GUILayout.HorizontalSlider(oldTime, 0.0, audio.clip.length);
if (oldTime != newTime) audio.time = newTime;

Well, I spent a while searching for a solution to what I thought was a serious problem…

No matter what I did, if I “paused” the audio, when I started it back up again at a different point, using the slider that this thread is about, a buffer of audio from when I paused would always play first. This was probably about a 1/8 sec. clip, which was just enough to be kind of annoying.

I tried a few alternative solutions, because I thought it was a limitation of AudioSource.Pause(). But no. In the IDE, the problem is unavoidable. However, the built projects do not exhibit this behavior. Now that I know, it’s not a big deal, but it would be nice if this worked better in the editor, so that no one else ever has to waste time trying to fix it like I did.

My girlfriend is making the visual elements of my GUI presently. I’ll put my jukebox the showcase asap. Thanks again for your help, guys.

http://teamuv.net/jessy/music/

As it is with the examples you guys gave, the playback starts up immediately after moving the slider. I would prefer for the playback to pause when dragging, and then play again when the thumb is released. I tried using GUIUtility.hotControl for this purpose, but I don’t really know to work with GUI Events. The manual tells me that it will instruct me, but it lies:

http://unity3d.com/support/documentation/Manual/Game%20Interface%20Elements.html

Any ideas?

This is what I was getting at with my example, however my explanation was rather bad.
Here’s the code. It’s quick and dirty, but it operates exactly like iTunes.

public class NewBehaviourScript : MonoBehaviour
{
    public float sliderPos;
    private Rect sliderRect = new Rect(10, 10, 300, 32);
    private bool UpdateAudioTime = false;

    void OnGUI()
    {
        GUILayout.BeginArea(sliderRect);
        if (Input.GetMouseButton(0))
        {
            sliderPos = GUILayout.HorizontalSlider(sliderPos, 0, audio.clip.length);
            UpdateAudioTime = true;
        }
        else
            GUILayout.HorizontalSlider(audio.time, 0, audio.clip.length);
        GUILayout.EndArea();
        if (UpdateAudioTime  Input.GetMouseButtonUp(0)) audio.time = sliderPos;
    }
}

EDIT: Almost exactly, like iTunes. It still keeps playing when you drag, and jumps to the new time on mouseup. I tried pausing during the drag, but when I started playing again, it replayed what was left in the buffer and then jumped to the new time.

Did you try building the project? This is behavior I always get in the editor. (See my second-to-last post.)

Anyway, thanks for the nice, simple solution. I’ll try to implement it asap!

Later…
I need this code to work only when dragging in the slider rectangle. This last example works if the slider is the only control. Otherwise, I need to figure out if I’m mousing over the correct control.