How to play video (VideoPlayer) with 3D game body?

How to play video with 3D game body?

When developers use Unity3D for craft simulation, AR or games, they often have to insert videos on 3D game bodies of various shapes as advertisements or teaching. So how to achieve this? This will use Unity's built-in video playback component- VideoPlayer .

Unity official website: https://docs.unity3d.com/Manual/class-VideoPlayer.html

Demo effect display

First look at the effect on the video (from left to right, it is plane, sphere, surface, cylinder):

How to play videos on the surface of 3D objects?

How to play videos on the surface of 3D objects

First create GameObject, add VideoPlayer component on GameObject: Insert picture description here
Then you can see that there are two options in Source, I currently use VideoClip.
1.VideoClip: can be directly dragged into the video in the Project view
2.URL: can be placed in the storage path of the video

Then there is an option worth mentioning below, RenderMode (rendering mode). Playing a video is actually very similar to a texture, because the MovieTexture used to play a video belongs to the subclass of texture. So here we want to play video on 3D objects, so choose MaterialOverride.

Check play on awake, run it, and you can see the video playing on the model.

Insert picture description here

How to code control VideoPlayer

1. Code control video playback, pause, monitor playback end, etc.

The video playback function has been completed above, and then we control the video through code: here
are some 视频播放、暂停,监听播放结束等方法.

        private VideoPlayer Vp;
        void Awake()
        {
    
    
            Vp = GetComponent<VideoPlayer>();
        }
        void Start()
        {
    
    
            Vp.loopPointReached += VideoEnd;

            Vp.Play();//播放视频
            Vp.Pause();//暂停视频
            Vp.Stop();//停止视频
            Vp.playbackSpeed = 1;//播放速度
        }
        /// <summary>
        /// 监听视频是否播放结束,结束时调用
        /// </summary>
        /// <param name="vp"></param>
        void VideoEnd(VideoPlayer vp)
        {
    
    
            Debug.Log("视频播放结束");
            Vp.Play();//重新播放视频
        }

2. How to monitor whether the 3D game body is in the field of view?

Video generally has sound. When doing AR projects, the video and sound effects on the 3D game body are usually turned off after the 3D game body goes out of bounds. So how to monitor whether the 3D game body is out of bounds?
Unity has a built-in method, the only condition for use is that the game body must have a Mseh component, because it is judged by whether the game body is rendered.
Shown below 3D游戏体是否在视野内的监测方法.

        //当游戏体第一次出界时调用,该游戏体身上必须带有Mseh组件
        private void OnBecameInvisible()
        {
    
    
            StopVideo();
            Debug.Log("出界");
        }
        //当游戏体第一次进入视野时调用,该游戏体身上必须带有Mseh组件
        private void OnBecameVisible()
        {
    
    
            OpenVideo();
            Debug.Log("视野内");
        }

Precautions

  1. Optional video types: .mp4, mov, .mpg, .mpeg, .avi and .asf
  2. Pro-test 3D model usage:
    1. The video will be played on each side of the model. For example, the video will be played on the 6 sides of a cube. In addition, if the model and the video scale are different, there will be stretching. Therefore, it is necessary to make art models and videos according to their needs.
    2. If it is found that the center of the video is not in the middle of the 3D model, it is a problem of the model's UV. You need to adjust the UV by art and place the video in the middle of the designated surface of the model.

How to play video on 2D Canvas

Playing video on the 2D interface and playing on the 3D game body, the principle and code are the same, the difference is that the Mesh component must be replaced with RawImage, and then the RenderMode option of VideoPlayer selects RenderTexture or other options.
I won’t talk about the details, there are many online, you can refer to the following URL:
https://blog.csdn.net/weixin_43367805/article/details/93178830
https://www.cnblogs.com/0kk470/p/10637034.html

Guess you like

Origin blog.csdn.net/qq_43505432/article/details/108663950