Unity3d two ways to achieve game video playback

Source: Internet
Author: User

Preparatory work

Because the video formats that can be recognized in Unity Are:. mov,. mpg,. mpeg,. asf, But in fact, when importing into unity in the last of these formats, it is a time-consuming operation to have the video transcoding in unity, which is a long-duration Action. and to ensure the successful completion of the video transcoding, we must also install the QuickTime Player tool for transcoding Video.

As required, after I installed Quicktime player, i imported the video war_video.mp4 into unity by dragging and dropping the result: "Movie importing requires Quicktime to be installed ... "this is because Unity's reliance on QuickTime Player has not yet taken Effect. By querying we can know that the video that was imported into unity was eventually transcoded to the . OGV format, which is a format that unity can recognize directly.

In solving the problem, I found a video transcoding tool that was able to transcode other formats of video into A. OGV format that unity can directly recognize, and does not rely on QuickTime player, and the quality of the exported video is higher than the QuickTime Transcoding. This tool is: Theoraconverter.

  

After the video transcoding succeeds, get a video file with the same name but a different suffix from the original video file: ***.ogv, import it into Unity. Since transcoding is not required, the resources are loaded quickly and you can preview the video in Inspector.

Body Preparation Work

There are two ways to play video games in Unity3d: the first is to play in the game object, for example, to create a plane polygon object in the game world, and the camera directly shines on the Face. The second is to play the video at the GUI level, which can onlybe seen in theGameview, and if you use HTC Vive at this point, you won't see the video in your Glasses.

  Playing the video is very much like a map, because the movietexture used to play the video belong to the sub-class of the map texture, and now we're going to learn about the two ways that video is played in Unity.

Unity supports playback video formats With. mov,. mpg,. mpeg,. mp4,. avi, and. Asf. Convert the video file into A. ogv format, then drag and drop the corresponding video file into Project view, and it will automatically generate the corresponding movietexture Object. Audio files will be generated if the video contains the Sound. 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.

Writing the test script

1 usingunityengine;2 usingsystem.collections;3 4  public classTestuseless:monobehaviour5 {6     //Movie Textures7      publicmovietexture movtexture;8 9     voidStart ()Ten     { one         //sets the main texture of the current object as a movie texture a          this. transform. getcomponent<renderer> (). material.maintexture =movtexture; -         //set the movie texture playback mode to loop -Movtexture.loop =true; the     } -     voidUpdate () -     { -  +     } -  +     voidOngui () a     { at         if(guilayout.button ("play/continue")) -         { -             //Play/resume Video -             if(!Movtexture.isplaying) -             { - Movtexture.play (); in             } -  to         } +  -         if(guilayout.button ("Pause Playback")) the         { *             //Pause Playback $ Movtexture.pause ();Panax Notoginseng         } -  the         if(guilayout.button ("Stop playing")) +         { a             //Stop playing the movtexture.stop (); +         } -     } $}
using the object drag form to assign a value to the MOV texture object, you can use it directly in the Script.

  

  The second way to play the video is based on the Gui. We delete the plane object we just created and the world directed light, bind the script directly to the camera object, and then we simply modify the game script just Now.

1 usingunityengine;2 usingsystem.collections;3 4  public classTestuseless:monobehaviour5 {6     //Movie Textures7      publicmovietexture movtexture;8 9     voidStart ()Ten     { one         //set the movie texture playback mode to loop aMovtexture.loop =true; -     } -  the     voidOngui () -     { -         //play video based on GUI -Gui. Drawtexture (NewRect (0,0, screen.width, screen.height), movtexture, scalemode.stretchtofill); +  -         if(guilayout.button ("play/continue")) +         { a             //Play/resume Video at             if(!Movtexture.isplaying) -             { - Movtexture.play (); -             } -         } -  in         if(guilayout.button ("Pause Playback")) -         { to             //Pause Playback + Movtexture.pause (); -         } the  *         if(guilayout.button ("Stop playing")) $         {Panax Notoginseng             //Stop playing - movtexture.stop (); the         } +     } a}

The principle of playing video in the GUI is to call the Drawtexture method directly from the gui, which is similar to the Mapping. The size of the currently playing video is the width of the screen, if you want to dynamically modify the width or height of the video directly modify the new Rect () video display Area.

Unity3d two ways to achieve game video playback

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.