How to play videos in Unity

1. First create a new Raw Image in the UI in the scene

You can hold down Alt and click the red arrow in the picture below to spread the Raw Image to the full screen of the game (you can also adjust the size yourself)


2. Add Video Player component to Raw Image

3. Create Render Texture in Assets or the folder you want

4. Drag the prepared video (the video format used here is mp4) into the project and make the following modifications

Here I named the new Render Texture 2, and the dragged video was also named 2 (you can name it casually, don’t care)

Here we see that the Render Texture is 256*256 by default. Here I change it to 1024*1024 to make the video clearer.

Then make the following modifications to the video. First check Transcode, and then select Square 1024 (consistent with Render Texture. Of course, other values ​​​​can also be used, but the corresponding values ​​​​in Render Texture must also be changed accordingly). I also selected it here. Stretch to fill the screen.

5. Drag Render Texture and video into the corresponding position of Raw Image

Then click run of the game to play the video!

Finally, attach the code for how to determine when the video has finished playing.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Video;

public class Video_change : MonoBehaviour
    double video_time, currentTime;
    //The video_img here is used to put RawImage. After mounting the script, just drag RawImage in.
    public GameObject video_img;
    void Start()
        video_time = video_img.GetComponent<VideoPlayer>().clip.length;

    // Update is called once per frame
    void Update()
        currentTime += Time.deltaTime;
        if (currentTime >= video_time)
            //Video playback ends, here you can write events after the video playback ends

Related Posts

Unity[DateTime]- How to add a validity period to the software

[Unity3D-UGUI Application] (4) Using UGUI pop-up window display model and pop-up window model interaction

unity3D terrain editor—Terrain

Unity exports the Android project and creates a new activity to place the privacy agreement

Unity game development: implementation of dialogue system

Unity3D official resource package Standard Assets import

Unity-Hololens-Vuforia development (picture recognition and model recognition)

Unity reads excel table data

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>