In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces how Unity3D plays game videos, which can be used for reference by interested friends. I hope you will gain a lot after reading this article. Let's take a look at it.
There are three ways to play game videos in Unity3D. The first is to play games in the game object, just like creating a Plane face object in the game world, and the camera shines directly on this face. The second is to play the video at the GUI level. Playing a video is actually very similar to a sticker, because the MovieTexture used to play the video belongs to a subcategory of the mapping Texture, and the third is to play Handheld.PlayFullScreenMovie on the phone. So in this chapter, we will take a good look at these three ways to play videos in Unity.
Unity supports .mov, .mpg, .mpeg, .mp4, .avi and .asf (MP4 is recommended for u3dnotes.com, otherwise it may not play properly). Simply drag the corresponding video file into the Project view, and it will automatically generate the corresponding MovieTexture object. As shown in the following figure, MOMO drags default_video.mp4 into the Project view and generates an audio file if the video contains audio. Because my video has no audio, no audio file is generated. Then create a Plane object video in the Hierarchy view and play on it, the Directional light world directional light is used to illuminate the entire game scene, and finally the Main Camera object shines straight on the Plane object.
Use object drag to assign a value to the Mov Texture object, so you can use it directly in the script. Let's take a look at the Test.cs script.
Test.cs
Using UnityEngine;using System.Collections; public class Test: MonoBehaviour {/ / Movie texture public MovieTexture movTexture; void Start () {/ / sets the main texture of the current object to Movie texture renderer.material.mainTexture = movTexture; / / sets Movie texture playback mode to cyclic movTexture.loop = true } void OnGUI () {if (GUILayout.Button ("play / continue")) {/ / play / continue playing video if (! movTexture.isPlaying) {movTexture.Play () }} if (GUILayout.Button ("pause playback") {/ / pause playback movTexture.Pause () } if (GUILayout.Button ("stop playback") {/ / stop playing movTexture.Stop ();}
As shown in the following figure, it is easy to play, pause and stop the operation after clicking the button. Default video size you can directly scale the Plane object plane in the editor, and if you need to use the dynamic zoom plane in the game:
Transform.localScale = new Vector (1pm 1pm 1)
The default scaling factor of the model is 1. Here, you can adjust the scaling coefficient of the plane X, Y, and Z, and the size of the plane will change, and so will the size of the corresponding video.
The second way to play video is based on GUI. You can delete the Plane object you just created and the world-oriented light, bind the script directly to the camera object, and then we can simply modify the game script.
Test.cs
Using UnityEngine;using System.Collections; public class Test: MonoBehaviour {/ / Movie texture public MovieTexture movTexture; void Start () {/ / set Movie texture playback mode to cyclic movTexture.loop = true } void OnGUI () {/ / paint movie texture GUI.DrawTexture (new Rect (0P0, Screen.width, Screen.height), movTexture,ScaleMode.StretchToFill) If (GUILayout.Button ("play / continue") {/ / play / continue to play the video if (! movTexture.isPlaying) {movTexture.Play () }} if (GUILayout.Button ("pause playback") {/ / pause playback movTexture.Pause () } if (GUILayout.Button ("stop playback") {/ / stop playing movTexture.Stop ();}
The principle of playing video in GUI is to call the DrawTexture method directly through GUI. Here and draw the map very much. Hey hey ~ at present, the size of the playing video is the width and height of the screen. If you want to dynamically modify the width or height of the video, you can directly modify the new Rect () video display area. As shown in the following figure, the video has been filled in the entire GUI. How about playing videos in Unity is easy? Wow, cut ~
Play video on mobile platform
After testing, the above methods can not play video in IOS and Android devices, we need to use another way to play video on mobile devices.
Using UnityEngine;using System.Collections; public class Test: MonoBehaviour {void OnGUI () {if (GUI.Button (new Rect (20, 10, 50, 200), "PLAY ControlMode.CancelOnTouch") {Handheld.PlayFullScreenMovie ("test.mp4", Color.black, FullScreenMovieControlMode.CancelOnInput) } if (GUI.Button (new Rect (20pr 90, 200je 25), "PLAY ControlMode.Full") {Handheld.PlayFullScreenMovie ("test.mp4", Color.black, FullScreenMovieControlMode.Full) } if (GUI.Button (new Rect (20 test.mp4 170) 200 25), "PLAY ControlMode.Hidden") {Handheld.PlayFullScreenMovie ("test.mp4", Color.black, FullScreenMovieControlMode.Hidden) } if (GUI.Button (new Rect (2050, 250, 25), "PLAY ControlMode.Minimal") {Handheld.PlayFullScreenMovie ("test.mp4", Color.black, FullScreenMovieControlMode.Minimal);}
1. Touch the screen video to close when the video is played
two。 IOS advanced control pops up when the video is played, and controls the video to stop playing full screen and so on.
3. The video cannot be stopped when it is played, and automatically closes when it is played once.
4. IOS advanced control pops up when the video is played, which can control the playback progress.
Note: place the video file under the Assets/StreamingAssets/ path and test that .MP4 is available. Play game videos smoothly on IOS and Android.
Project download: http://vdisk.weibo.com/s/gb4Lx
Accelerated video playback (PC only)
Today, a friend asked me how to speed up the video playback, I also learned, but foreigners all said that this is a BUG hehe.
The script is bound to the camera object and adds an AudioSource component to the camera.
The code is simple, so I won't comment it.
Using UnityEngine;using System.Collections; public class NewBehaviourScript: MonoBehaviour {public MovieTexture mov; void Start () {audio.clip = mov.audioClip; audio.Play (); mov.Play () } void OnGUI () {if (GUI.Button (new Rect (310J 0je 100je 50), "2x play") {audio.pitch = 2f } if (GUI.Button (new Rect (410memo 100jue 50), "1 time playback") {audio.pitch = 1f;} GUI.DrawTexture (new Rect (0mem0300300), mov) }} Thank you for reading this article carefully. I hope the article "how to play Game Video on Unity3D" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support and follow the industry information channel. More related knowledge is waiting for you to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.