Hello everyone, I'm Qin Yuanpei. Welcome to follow my blog. My blog address is blog.csdn.net/qinyuanpei.
Today, we will create an instance for video playback in Unity3D. First, unitysupported video formats include .mov?.mpg=.mpeg=.mp4,. avi, And. asf. You only need to put these files in the project folder to import video files. In the Window system, we need to use Apple's QuickTime software to smoothly import video files to Unity. If the video file contains an audio file, the corresponding audio file is generated after Unity is imported. There are two video playback methods in Unity3D: texture Paster based and GUI based. We will explain the following:
1. Texture Based textures
View plaincopyprint?
- Using UnityEngine;
- Using System. Collections;
- Public class MovieByTexture: MonoBehaviour {
- // Movie texture map
- Public MovieTexture mMovie;
- // Movie audio source
- Private AudioSource mAudio;
- // Media information Definition
- Private string mediaName; // media name
- Private string mediaLength; // media Length
- Void Start ()
- {
- MAudio = GetComponent <AudioSource> ();
- // Set the main texture of the current object to a movie texture map.
- Renderer. material. mainTexture = mMovie;
- // Set the playback mode of the movie texture to loop.
- MMovie. loop = true;
- // Start playing a movie
- MMovie. Play ();
- // Start playing music
- MAudio. Play ();
- }
- Void Update ()
- {
- // Update media information here
- MediaName = mMovie. name;
- MediaLength = mMovie. duration. ToString ();
- }
- Void OnGUI ()
- {
- // Playback control
- If (GUILayout. Button ("play", GUILayout. Height (30 )))
- {
- MMovie. Play ();
- MAudio. Play ();
- }
- // Suspend control
- If (GUILayout. Button ("Suspend", GUILayout. Height (30 )))
- {
- MMovie. Pause ();
- MAudio. Pause ();
- }
- // Stop control
- If (GUILayout. Button ("stop", GUILayout. Height (30 )))
- {
- MMovie. Stop ();
- MAudio. Stop ();
- }
- // Draw text information
- GUILayout. Label ("media name:" + mediaName, GUILayout. Height (30 ));
- GUILayout. Label ("Media length:" + mediaLength, GUILayout. Height (30 ));;
- }
- }
2. GUI-based
[Csharp]View plaincopyprint?
- Using UnityEngine;
- Using System. Collections;
- Public class MovieByGUI: MonoBehaviour {
- // Movie texture map
- Public MovieTexture mMovie;
- // Movie audio source
- Private AudioSource mAudio;
- // Media information Definition
- Private string mediaName; // media name
- Private string mediaLength; // media Length
- Void Start ()
- {
- MAudio = GetComponent <AudioSource> ();
- // Set the main texture of the current object to a movie texture map.
- Renderer. material. mainTexture = mMovie;
- // Set the playback mode of the movie texture to loop.
- MMovie. loop = true;
- // Start playing a movie
- MMovie. Play ();
- // Start playing music
- MAudio. Play ();
- }
- Void Update ()
- {
- // Update media information here
- MediaName = mMovie. name;
- MediaLength = mMovie. duration. ToString ();
- }
- Void OnGUI ()
- {
- // Draw a movie texture map
- GUI. DrawTexture (new Rect (0, 0, Screen. width, Screen. height), mMovie );
- // Playback control
- If (GUILayout. Button ("play", GUILayout. Height (30 )))
- {
- MMovie. Play ();
- MAudio. Play ();
- }
- // Suspend control
- If (GUILayout. Button ("Suspend", GUILayout. Height (30 )))
- {
- MMovie. Pause ();
- MAudio. Pause ();
- }
- // Stop control
- If (GUILayout. Button ("stop", GUILayout. Height (30 )))
- {
- MMovie. Stop ();
- MAudio. Stop ();
- }
- // Draw text information
- GUILayout. Label ("media name:" + mediaName, GUILayout. Height (30 ));
- GUILayout. Label ("Media length:" + mediaLength, GUILayout. Height (30 ));;
- }
- }
The code is very simple and I will not go into details here. We can directly create a Plane in the scenario, drag and drop the script to this object, and specify the audio file and video file to play the video. Final effect:
Today's content is like this. I don't know what to write recently. If you want to know anything, leave a message for me.
If you like my blog, please remember my name: Qin Yuanpei. My blog address is blog.csdn.net/qinyuanpei.
Reprinted please indicate the source, Author: Qin Yuanpei, the source of this article: http://blog.csdn.net/qinyuanpei/article/details/24130347