[Unity Tutorial] Mastering Video Playback: A Comprehensive C# Guide

2024.09.22 / Unity / 繁體中文
Video Player is Unity’s built-in video playback component that allows developers to control video playback, pausing, stopping, swapping, and timing through C# scripts. This article will demonstrate how to control the Video Player using C# scripts and provide syntax examples for many common functions.

Presetting

Adding a Render Texture

  1. Right-click in the Textures folder > Create > Render Texture.
  2. Modify the Render Texture’s Size to 1920 x 1080。

Adding a Video Player

  1. Right-click in the scene > Video > Video Player.
  2. Drag the video you want to play to Video Clip.
  3. Uncheck Play On Awake.
  4. Drag the Render Texture to Target Texture.

Adding a Raw Image

  1. Modify the Game View resolution to 1920 x 1080.
  2. Right-click in the scene > UI > Raw Image.
  3. Click the Anchor Present in the upper left corner of the Raw Image.
  4. Hold Alt and click the bottom right option of the Anchor Present to enlarge the Raw Image to full screen.
  5. Click 2D in the Scene View to enable 2D mode.
  6. Drag the Render Texture to the Texture of the Raw Image.

Adding a C# Script

  1. Right-click in the Scripts folder > Create > C# Script and name it VideoController.

  1. Drag VideoController under the Video Player. Now we have completed all the presets.

C# Scripting

Playing a Video Once

Play the video when the project starts.

using UnityEngine;
using UnityEngine.Video;

public class VideoController : MonoBehaviour
{
    private VideoPlayer _videoPlayer;

    private void Awake() => _videoPlayer = GetComponent<VideoPlayer>();

    private void Start()
    {
        _videoPlayer.Play();
    }
}

Looping a Video

Play the video when the project starts and automatically replay when the video finishes.

using UnityEngine;
using UnityEngine.Video;

public class VideoController : MonoBehaviour
{
    private VideoPlayer _videoPlayer;

    private void Awake() => _videoPlayer = GetComponent<VideoPlayer>();

    private void Start()
    {
        _videoPlayer.isLooping = true;
        _videoPlayer.Play();
    }
}

Playing Multiple Videos Once

Play the first video when the project starts. When the video finishes, automatically play the next video until all the videos in Clips are finished.

using UnityEngine;
using UnityEngine.Video;

public class VideoController : MonoBehaviour
{
    public VideoClip[] Clips;

    private VideoPlayer _videoPlayer;
    private int _videoIndex;

    private void Awake() => _videoPlayer = GetComponent<VideoPlayer>();

    private void Start()
    {
        _videoIndex = 0;
        _videoPlayer.clip = Clips[0];
        _videoPlayer.loopPointReached += NextVideo;
        _videoPlayer.Play();
    }

    private void NextVideo(VideoPlayer _)
    {
        _videoIndex++;
        if (_videoIndex >= Clips.Length)
        {
            return;
        }

        _videoPlayer.clip = Clips[_videoIndex];
        _videoPlayer.Play();
    }
}

Looping Multiple Videos

Play the first video when the project starts. When the video finishes, automatically play the next video. If all the videos in Clips are finished, replay from the first video and loop infinitely.

using UnityEngine;
using UnityEngine.Video;

public class VideoController : MonoBehaviour
{
    public VideoClip[] Clips;

    private VideoPlayer _videoPlayer;
    private int _videoIndex;

    private void Awake() => _videoPlayer = GetComponent<VideoPlayer>();

    private void Start()
    {
        _videoIndex = 0;
        _videoPlayer.clip = Clips[0];
        _videoPlayer.loopPointReached += NextVideo;
        _videoPlayer.Play();
    }

    private void NextVideo(VideoPlayer _)
    {
        _videoIndex++;
        if (_videoIndex >= Clips.Length)
        {
            _videoIndex = 0;
        }

        _videoPlayer.clip = Clips[_videoIndex];
        _videoPlayer.Play();
    }
}

相關文章

Ted Liou

雲科碩士在讀中,專注於 Unity C#、TouchDesigner 技術,常把技術筆記分享到部落格,偶爾還直接挪用文章來當教材的研究生。

Comment

Let me know what you think!