Last active
March 31, 2021 14:23
-
-
Save paulhayes/f70f8ec2790cd0ff1e6857c580c1a7ce to your computer and use it in GitHub Desktop.
Extends Unity VideoPlayer with methods to display the videos first frame, with with either a yielding coroutine or callback function when the video is ready to be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using UnityEngine.Video; | |
public static class VideoPlayerExtentions | |
{ | |
public static void ShowFirstFrame(this VideoPlayer player, System.Action onCompleteCallback) | |
{ | |
VideoPlayer.FrameReadyEventHandler frameReadyHandler = null; | |
bool oldSendFrameReadyEvents = player.sendFrameReadyEvents; | |
frameReadyHandler = (source,index)=>{ | |
player.sendFrameReadyEvents = oldSendFrameReadyEvents; | |
player.frameReady -= frameReadyHandler; | |
onCompleteCallback(); | |
}; | |
player.frameReady += frameReadyHandler; | |
player.sendFrameReadyEvents = true; | |
player.Prepare(); | |
player.StepForward(); | |
} | |
public static IEnumerator ShowFirstFrame(this VideoPlayer player) | |
{ | |
VideoPlayer.FrameReadyEventHandler frameReadyHandler = null; | |
bool frameReady = false; | |
bool oldSendFrameReadyEvents = player.sendFrameReadyEvents; | |
frameReadyHandler = (source,index)=>{ | |
frameReady = true; | |
player.frameReady -= frameReadyHandler; | |
player.sendFrameReadyEvents = oldSendFrameReadyEvents; | |
}; | |
player.frameReady += frameReadyHandler; | |
player.sendFrameReadyEvents = true; | |
player.Prepare(); | |
player.StepForward(); | |
while(!frameReady){ | |
yield return null; | |
} | |
} | |
} |
Ah okay I understand the logic now behind how it works.
I ended up coding something very similar to your suggestions with saving the initial frame, although I'm gonna experiment with AssetDatabase.ImportAsset(path) now, I hadn't seen that earlier.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This script is for displaying the first frame of video until play is initiated by user interaction.