#YouTube Slider
1 messages Β· Page 1 of 1 (latest)
Actually I would like to do smth similar to YouTube Slider. The video's time is changed just after dragging.
Does it makes sense with if (!Input.GetMouseButton(0)) ?
YouTube Slider
That would make sense to me - though I may not be the best sounding board based on my limited experience π
I think one potential complication to either approach would be if you intend to support keyboard input for the slider
this code does not work
timeSlider.onValueChanged.AddListener(_ =>
{
if (!Input.GetMouseButtonDown(0))
{
Debug.Log("changed");
audioSource.time = timeSlider.value;
}
});
yeah, I do already have KeyCode.LeftArrow and KeyCode.RightArrow to remote
I think it would have to be Input.GetMouseButton(0) there, as GetMouseButtonDown() is tied to the frame
yeah, I have already tried with Input.GetMouseButton(0), but it works just the same as without it:
timeSlider.onValueChanged.AddListener(_ =>
{
audioSource.time = timeSlider.value;
});
hmmm
I'll play around a bit myself - I'm curious
Ah right - because the value won't change after the mouse button is released π
yeah
I guess I can try to do smth with OnMouseButtonUp
@kind fulcrum this seems to work fairly well, in very limited testing:
public class YouTubeSlider : MonoBehaviour, IEndDragHandler, IBeginDragHandler
{
public Slider timeSlider;
public AudioSource audioSource;
private float previousValue;
public void OnEndDrag(PointerEventData eventData)
{
if (timeSlider.value == previousValue)
return;
audioSource.time = timeSlider.value;
}
public void OnBeginDrag(PointerEventData eventData)
{
previousValue = timeSlider.value;
}
}
I have it attached to the slider GO
wait, I gonna try it
also the solution I have just made:
timeSlider.onValueChanged.AddListener(_ =>
{
StartCoroutine(ChangeTime());
IEnumerator ChangeTime()
{
yield return new WaitUntil(() => !Input.GetMouseButton(0));
Debug.Log("worked? haha");
audioSource.time = timeSlider.value;
}
});
Ah very cool! I did not know you could WaitUntil() single-frame inputs like that ποΈ
yeah
but wait
public void OnEndDrag(PointerEventData eventData)
{
if (timeSlider.value == previousValue) return;
audioSource.time = timeSlider.value;
}
public void OnBeginDrag(PointerEventData eventData)
{
previousValue = timeSlider.value;
}
why should it work on timeSlider?
You mean why attached to like the slider GO instead of the handle?
oh so
so it should be attached to the slider?
that makes sense then
I did anyway - I figured it would be nice to work with those events whether they are triggered on the handle, or the background/bar thingy. That way it sort of automatically accounts for the case of starting the drag at a random position on the bar
yeah
actually
I have no let the previous code and it works, so the music gets skipped when dragging the slider
but
i cannot drag it physically when I change the slider's value in Update
timeSlider.value = (int)audioSource.time;
I just cannot drag it
private void Start()
{
timeSlider.onValueChanged.AddListener(_ =>
{
Debug.Log("onValueChanged");
StartCoroutine(ChangeTime());
IEnumerator ChangeTime()
{
yield return new WaitUntil(() => !Input.GetMouseButton(0));
Debug.Log("worked? haha");
audioSource.time = timeSlider.value;
}
});
}
private void Update()
{
timeSlider.value = (int)audioSource.time;
timeInput.text = $"{ParseMusicTime(audioSource.time)} / {musicParsedTime}";
}
I guess I should use additional bool ??
Doesn't that code start a big pile of ChangeTime() coroutines running in parallel?
what do you mean?
Every time onValueChanged() triggers, a new coroutine is started - I would think when you release left mouse you'd get a Debug.Log("worked? haha"); for each of the coroutines which was started?
But I guess that could also be mitigated with the new bool π
haha yeah, lemme try this to
that's nice issue though
and this is big minus for game's productivity
lmao
now I have broken it π
I guess I even know why
timeSlider.onValueChanged.AddListener(_ =>
{
changingTime = true;
if (canStartChangeTimeCoroutine) StartCoroutine(ChangeTime());
IEnumerator ChangeTime()
{
canStartChangeTimeCoroutine = false;
yield return new WaitUntil(() => !Input.GetMouseButton(0));
Debug.Log("worked? haha");
audioSource.time = timeSlider.value;
changingTime = false;
}
});
The only suspicious thing I see there is canStartChangeTimeCoroutine is never reset to true.
yeah, thank you
missed it
But I think changingTime could also fulfill canStartChangeTimeCoroutine's purpose:
timeSlider.onValueChanged.AddListener(_ =>
{
if (!changingTime) StartCoroutine(ChangeTime());
IEnumerator ChangeTime()
{
changingTime = true;
yield return new WaitUntil(() => !Input.GetMouseButton(0));
Debug.Log("worked? haha");
audioSource.time = timeSlider.value;
changingTime = false;
}
});
lemme try
yeah, that works, thank you
I have also thought about it, but was too stupid to implement
π that's half of my workday
@dim cedar thank you for your time and help today, I really do appreciate it
Np - I like a good problem :)