Last active
March 27, 2025 16:07
-
-
Save radiatoryang/95a9865992a78b33d3099f595863eb7f to your computer and use it in GitHub Desktop.
short Unity C# script example for smoothly cutting / playing a playllist of VideoClips, by using one VideoPlayer (activeCam) to play the video and another VideoPlayer (otherCam) to cue up the next video... MIT License.
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 class SmoothVideoPlaylist : MonoBehaviour { | |
// to smoothly cut between two videos, we need two VideoPlayers | |
// make sure you create two VideoPlayers and assign them in the Inspector | |
public VideoPlayer activeCam, otherCam; | |
// edit this in the inspector, and fill it with your video clips | |
public List<VideoClip> playlist = new List<VideoClip>(); | |
// internal var, keeps track of whether there's another clip cued up | |
VideoClip nextClip; | |
void Start () { | |
// play the first video in the playlist | |
PrepareNextPlaylistClip(); | |
SwitchCams(activeCam); | |
// setup an event to automatically call SwitchCams() when we finish playing | |
activeCam.loopPointReached += SwitchCams; | |
otherCam.loopPointReached += SwitchCams; | |
} | |
void Update () { | |
// when the current video is 0.1 seconds away from ending, prepare the next video clip on otherCam | |
if ( nextClip == null && activeCam.time >= activeCam.clip.length-0.1 ) { | |
PrepareNextPlaylistClip(); | |
} | |
// FAST FORWARD CHEAT, hold down F to speed-up video | |
if ( Input.GetKey(KeyCode.F) ) { | |
Time.timeScale = 16f; | |
activeCam.playbackSpeed = 16f; | |
otherCam.playbackSpeed = 16f; | |
} else { | |
Time.timeScale = 1f; | |
activeCam.playbackSpeed = 1f; | |
otherCam.playbackSpeed = 1f; | |
} | |
} | |
// swaps the VideoPlayer references, so activeCam is always the active VideoPlayer | |
void SwitchCams (VideoPlayer thisCam) { | |
activeCam = otherCam; | |
otherCam = thisCam; | |
activeCam.targetCameraAlpha = 1f; | |
otherCam.targetCameraAlpha = 0f; | |
Debug.Log("new clip: " + nextClip.name ); | |
nextClip = null; | |
} | |
// cues up the next video clip in the playlist | |
void PrepareNextPlaylistClip () { | |
nextClip = playlist[0]; | |
otherCam.clip = nextClip; | |
otherCam.Play(); | |
playlist.RemoveAt(0); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This works beautifully. I wondered, how do I click on an UI button to go to next clip or go back to previous clip as listed in the inspector?