Hi,
i have implemented some video controls for my video player according to this tutorial: Unity Video Player with Time Scrub and Controls - YouTube
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