Unity basic video component VideoPlayer, video playback and control

In Unity, the video playback function has a wide range of applications, the following are some common uses of video playback in Unity:

      Game introductions and cutscenes: Use video playouts to add compelling video to game starts or cutscenes to create atmosphere and interest in the game. This method can enhance the game experience by showing the game story, background introduction of the game, or video of important episodes.

       Game menus and user interfaces: By embedding video in game menus and user interfaces, it is possible to provide a more engaging and interactive interface. For example, play a game trailer, featured clip, or sample gameplay video in the game menu to show players the features and gameplay of the game.

       Educational and training applications: Video playback is useful in educational and training applications. Live and intuitive learning experiences can be provided by playing instructional videos in virtual classrooms, training modules or educational games. Videos can be used to demonstrate the operation of experiments, demonstrate concepts, explain complex procedures, or provide demonstrations.

      Interactive virtual reality (VR) and augmented reality (AR) experiences: In VR and AR applications, video playback can be used to enhance the realism and interactivity of the virtual world. For example, in a virtual tourism application, the tourism experience can be simulated by playing a video of a real scene. In AR applications, the integration of virtual content can be achieved by playing videos that match the real environment.

      User Feedback and Game Progression: By playing specific video clips in the game, it is possible to provide feedback on game progress or mission completion. This can include reward videos, mission completion animations, or key clips that advance the story. In this way, players can get instant visual and audio feedback, increasing the immersion of the game.

     All in all, video playback plays many important roles in Unity, from enhancing the gaming experience to educational training applications, and opening up more possibilities for user interfaces and interactive virtual reality. By using the VideoPlayer component of Unity, you can easily implement the video playback function in your project, bringing users a richer and more diverse experience. 

   First of all, let's take a look at the video playback component VideoPlayer 

Source : source of video playback, there are two options: video clip and URL, video clip can directly select video clip, URL needs to be put into video link

Play when wake up : Whether to play directly when the program is running, the video is played by default after checking

Wait for the first frame : Whether to pre-cache a frame, the default is checked, if not checked, there will be no video waiting when there is no cache during playback

Loop : Replay after the video finishes playing

Playback speed : video playback speed

Rendering mode : The rendering mode during video playback. By default, the rendering texture (UI) and material overlay (in the scene) are used. To use the rendering texture, you need to right-click to create a new renderer texture. The material overlay requires a renderer texture + shader

Audio output mode : the output mode of the sound, you can choose the audio source (you need to specify an audio playback component) or directly (directly use the sound of the video, this is the default) 

Render Texture is also indispensable in video playback

       A renderer texture is a special texture that records the rendering result of the camera and saves it as an image. This texture can be used for real-time monitoring, post-production special effects, texture grabbing, and multi-camera rendering. It allows us to view the images captured by the camera in real time, apply various special effects, capture textures and apply them to other objects, achieve dynamic texture effects, and support multiple cameras to render different scenes at the same time. Renderer textures give us more creativity and flexibility, enhancing the visuals of games and applications.

      When we want to play video in games or applications, we usually use the VideoPlayer component to load and control video files. However, rendering video directly to the screen may not be sufficient for our needs, as we may want to play the video at a specific location in the game scene, or apply video content to the surface of a game object.

      This is where renderer textures come into play. We can create a renderer texture and set the output of the VideoPlayer component to this texture, so that the content of the video playback will be rendered to this texture. Then, we can apply the texture to the material of the game object, or display the texture in the UI element, so as to achieve the playback effect of the video. 

Play video in scene

     If we need to play the video in the scene, we can create a new plane as the carrier for displaying the video, and then add the video playback component

    After that, we right-click to create a renderer texture, and directly drag the renderer texture to the plane after creation 

        At this time, a material ball will be automatically generated and assigned to this plane

Then we find the video playback component and assign the video to be played. You can see that the Renderer of the video playback component is our newly created Plane, and then click Run to see the video playback (the mode at this time is material coverage), of course we You can also adjust the zoom of this plane to adjust the size of the video being played 

Play video in UI

First right click in the hierarchy panel - UI - original image (RawImage)

After creation, we can directly drag the newly created renderer texture to the texture under the RawImage component

Then we add the video playback component (VideoPlayer), specify the video, then set the rendering mode to the renderer texture, assign the renderer texture, and click Run to play 

Let's take a look at how to control the video component with code, first look at the commonly used properties and methods 

//playOnAwake       唤醒时播放,程序运行时是否默认就播放视频
//isLooping         循环播放,播放完成后是否重复播放
//playbackSpeed     播放速度,默认从0-10
//waitForFirstFrame 等待第一帧
//isPlaying         当前是否在播放
//isPaused          当前是否暂停
//Stop()            停止播放,每次使用停止播放再次开始播放时候都会重新播放视频
//Pause()           暂停播放,只是咱停播放,再次开始播放时会从暂停的地方开始播放
//Play()            开始播放,使用停止或者暂停播放可以通过Play重新开始播放 

 The following code is how to control some parameters of the video playback component, and control the playback and stop of the video

using UnityEngine;
using UnityEngine.Video;//需要引入视频组件的命名空间
public class VideoPlayerTest : MonoBehaviour
{

    //视频播放组件
    private VideoPlayer _videoplayer;

    void Awake()
    {
        //获取自身的视频播放组件
        _videoplayer = GetComponent<VideoPlayer>();
    }

    void Start()
    {
        //设置运行时播放
        _videoplayer.playOnAwake = true;
        //设置是否循环播放
        _videoplayer.isLooping = true;
        //设置视频倍速
        _videoplayer.playbackSpeed = 1.5f;
        //设置预先缓存一帧
        _videoplayer.waitForFirstFrame = true;
        //如果音频输出模式指定了音频组件可以通过代码控制
        //控制视频静音
        _videoplayer.GetTargetAudioSource(0).mute = false;
        //控制视频音量大小
        _videoplayer.GetTargetAudioSource(0).volume = 0.5f;
    }
    void Update()
    {
        //按下键盘P键来判断当前视频是否在播放,如果是播放状态就停止,如果是停止状态就开始播放
        if (Input.GetKeyDown(KeyCode.P))
        {
            if (_videoplayer.isPlaying)
            {
                _videoplayer.Stop();
            }
            else
            {
                _videoplayer.Play();
            }

        }
        //按下键盘A键来判断当前视频是否在暂停,如果是播暂停就播放,如果是播放就暂停
        if (Input.GetKeyDown(KeyCode.A))
        {
            if (_videoplayer.isPaused)
            {
                _videoplayer.Play();
            }
            else
            {
                _videoplayer.Pause();
            }

        }
    }
}

Guess you like

Origin blog.csdn.net/qq_36592993/article/details/131280740