There are two ways to play video game in Unity3d, the first is to play in the game object, just like creating a plane object in the game world, and the camera is directly shining on the surface. The second is to play the video at the GUI level. Playing the video is very similar to the map, because the movietexture used to play the video belong to the sub-class of the map texture, so we'll learn about the two ways in which unity plays video. Wow, ka-hoo.
Unity supports playback video formats with. mov,. mpg,. mpeg,. mp4,. avi, and. asf. Simply drag and drop the corresponding video file into Project view, and it will automatically generate the corresponding Movietexture object. As shown, Momo drag the Default_video.mp4 into Project view, and if the video contains audio, it will generate audio files, because my video does not have audio, so I did not generate an audio file. Then create a Plane object video in the hierarchy view that will play on top of it, directional light world directed lights to illuminate the entire game scene, and finally the main camera object will be directly irradiated on the plane object.
Using the object drag form to assign a value to the Mov texture object, then you can use it directly in the script, let's 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 for the movie texture renderer.material.mainTexture = movtexture; Set the movie texture playback mode to loop Movtexture.loop = true; } void Ongui () { if (Guilayout.button ("Play/Continue") {// play/Resume video if (!movtexture.isplaying) { movtexture.play (); } } if (Guilayout.button ("Pause play")) { //Pause play movtexture.pause (); } if (Guilayout.button ("Stop playing")) { //stop playing movtexture.stop () ; }}}
As shown, it is easy to play, pause, and stop operations after clicking the button. Default video size You can scale the plane object plane directly in the editor, and if you need to dynamically scale the plane usage in the game run:
1 |
transform.localScale = new Vector(1,1,1); |
The model default scaling factor is 1, here you can adjust the plane x, Y, z Three direction of the scaling factor, the size of the plane will change, the corresponding video size will change accordingly.
The second way to play the video is based on the GUI. You can delete the plane object you just created and the world directed light, bind the script directly to the camera object, and then we'll 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 loop Movtexture.loop = true; } void Ongui () { //Draw the movie texture GUI. Drawtexture (New Rect (0,0, Screen.width, screen.height), Movtexture,scalemode.stretchtofill); if (Guilayout.button ("Play/Continue") {// play/Resume video if (!movtexture.isplaying) { Movtexture.play (); } } if (Guilayout.button ("Pause play")) { //Pause play movtexture.pause (); } if (Guilayout.button ("Stop playing")) { //stop playing movtexture.stop () ; }}}
The principle of playing video in the GUI is to call the Drawtexture method directly through the GUI, here and draw the map very much think of it ~ hehe ~ The current playback video size is the width of the screen height, if you want to dynamically modify the width or height of the video directly modify the new Rect () video display area can be, as shown in , the video is filled with the full GUI.
Play Video on mobile platforms
The tests above are not able to play video on iOS and Android devices, and we need to use another way to play on mobile devices.
Using unityengine;using System.collections;public class Test:monobehaviour { void Ongui () { if (GUI. button (new Rect (20,10,200,50), "PLAY Controlmode.cancelontouch")) { Handheld.playfullscreenmovie (" Test.mp4 ", Color.Black, Fullscreenmoviecontrolmode.canceloninput); } if (GUI. button (new Rect (20,90,200,25), "PLAY controlmode.full")) { Handheld.playfullscreenmovie ("Test.mp4", Color.Black, fullscreenmoviecontrolmode.full); } if (GUI. button (new Rect (20,170,200,25), "PLAY Controlmode.hidden")) { Handheld.playfullscreenmovie ("Test.mp4", Color.Black, Fullscreenmoviecontrolmode.hidden); } if (GUI. button (new Rect (20,250,200,25), "PLAY controlmode.minimal")) { Handheld.playfullscreenmovie ("Test.mp4", Color.Black, Fullscreenmoviecontrolmode.minimal);}}}
1. Touch screen video off during video playback
2. When the video player pops up the iOS advanced control, controls the video pause to play fullscreen, and so on.
3. The video cannot be stopped when it plays, and automatically shuts down when it plays
4. When the video is playing, the iOS advanced control is ejected to control the playback progress.
Note: Place the video file under the assets/streamingassets/path and test it. MP4 available. Play video games smoothly on iOS and Android.
Video accelerated playback (for PC only)
The script binds to the camera object and adds the Audiosource component to the camera.
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 (310,0,100,50), "twice Times Playback")) { audio.pitch = 2f; } if (GUI. button (new Rect (410,0,100,50), "1 time Times Playback")) { audio.pitch = 1f; } Gui. Drawtexture (New Rect (0,0,300,300), MOV); }}
Unity3d two ways to play video