- Home /
video progress bar is not smooth OnDrag().
Hi,
i have implemented some video controls for my video player according to this tutorial: https://www.youtube.com/watch?time_continue=4&v=9LwOmMzOrp4
Here is the code:
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
using UnityEngine.Video;
public class VideoProgressBar : MonoBehaviour, IDragHandler, IPointerDownHandler
{
[SerializeField]
protected VideoPlayer videoPlayer;
private Image progress;
private void Awake()
{
progress = GetComponent<Image>();
}
private void Update()
{
if (videoPlayer.frameCount > 0)
progress.fillAmount = (float)videoPlayer.frame / (float)videoPlayer.frameCount;
}
public void OnDrag(PointerEventData eventData)
{
TrySkip(eventData);
}
public void OnPointerDown(PointerEventData eventData)
{
TrySkip(eventData);
}
private void TrySkip(PointerEventData eventData)
{
if (RectTransformUtility.ScreenPointToLocalPointInRectangle(progress.rectTransform, eventData.position, null, out Vector2 localPoint))
{
float pct = Mathf.InverseLerp(progress.rectTransform.rect.xMin, progress.rectTransform.rect.xMax, localPoint.x);
SkipToPercent(pct);
}
}
private void SkipToPercent(float pct)
{
var frame = videoPlayer.frameCount * pct;
videoPlayer.frame = (long)frame;
}
}
It is working fine and does what it should but sadly the fillAmount of the image which indicates the progress of the video doesn't seem to update smoothly when dragging across the progress bar. It only updates when I finished dragging and don't move the mouse for a split second.
Is there a way to make it update smoothly in so it follows the progress equals the mouse position even while dragging?
Thanks in advance :)
Answer by UnityLib43 · Feb 11, 2019 at 05:26 PM
Hello @MSachs you can use the slider component in the ui elements and make it as a dynamic video progress bar by linking the value of the slider to the video time ,also check this asset here it contains too much examples : https://assetstore.unity.com/packages/slug/138798
That is actually not a bad idea! Didn't think about that at all. Thanks a lot I will try that!