Unity3D game development: video playback in Unity3D

Source: Internet
Author: User

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?

 
  1. Using UnityEngine;
  2. Using System. Collections;
  3. Public class MovieByTexture: MonoBehaviour {
  4. // Movie texture map
  5. Public MovieTexture mMovie;
  6. // Movie audio source
  7. Private AudioSource mAudio;
  8. // Media information Definition
  9. Private string mediaName; // media name
  10. Private string mediaLength; // media Length
  11. Void Start ()
  12. {
  13. MAudio = GetComponent <AudioSource> ();
  14. // Set the main texture of the current object to a movie texture map.
  15. Renderer. material. mainTexture = mMovie;
  16. // Set the playback mode of the movie texture to loop.
  17. MMovie. loop = true;
  18. // Start playing a movie
  19. MMovie. Play ();
  20. // Start playing music
  21. MAudio. Play ();
  22. }
  23. Void Update ()
  24. {
  25. // Update media information here
  26. MediaName = mMovie. name;
  27. MediaLength = mMovie. duration. ToString ();
  28. }
  29. Void OnGUI ()
  30. {
  31. // Playback control
  32. If (GUILayout. Button ("play", GUILayout. Height (30 )))
  33. {
  34. MMovie. Play ();
  35. MAudio. Play ();
  36. }
  37. // Suspend control
  38. If (GUILayout. Button ("Suspend", GUILayout. Height (30 )))
  39. {
  40. MMovie. Pause ();
  41. MAudio. Pause ();
  42. }
  43. // Stop control
  44. If (GUILayout. Button ("stop", GUILayout. Height (30 )))
  45. {
  46. MMovie. Stop ();
  47. MAudio. Stop ();
  48. }
  49. // Draw text information
  50. GUILayout. Label ("media name:" + mediaName, GUILayout. Height (30 ));
  51. GUILayout. Label ("Media length:" + mediaLength, GUILayout. Height (30 ));;
  52. }
  53. }

 

 

2. GUI-based

 

[Csharp]View plaincopyprint?
  1. Using UnityEngine;
  2. Using System. Collections;
  3. Public class MovieByGUI: MonoBehaviour {
  4. // Movie texture map
  5. Public MovieTexture mMovie;
  6. // Movie audio source
  7. Private AudioSource mAudio;
  8. // Media information Definition
  9. Private string mediaName; // media name
  10. Private string mediaLength; // media Length
  11. Void Start ()
  12. {
  13. MAudio = GetComponent <AudioSource> ();
  14. // Set the main texture of the current object to a movie texture map.
  15. Renderer. material. mainTexture = mMovie;
  16. // Set the playback mode of the movie texture to loop.
  17. MMovie. loop = true;
  18. // Start playing a movie
  19. MMovie. Play ();
  20. // Start playing music
  21. MAudio. Play ();
  22. }
  23. Void Update ()
  24. {
  25. // Update media information here
  26. MediaName = mMovie. name;
  27. MediaLength = mMovie. duration. ToString ();
  28. }
  29. Void OnGUI ()
  30. {
  31. // Draw a movie texture map
  32. GUI. DrawTexture (new Rect (0, 0, Screen. width, Screen. height), mMovie );
  33. // Playback control
  34. If (GUILayout. Button ("play", GUILayout. Height (30 )))
  35. {
  36. MMovie. Play ();
  37. MAudio. Play ();
  38. }
  39. // Suspend control
  40. If (GUILayout. Button ("Suspend", GUILayout. Height (30 )))
  41. {
  42. MMovie. Pause ();
  43. MAudio. Pause ();
  44. }
  45. // Stop control
  46. If (GUILayout. Button ("stop", GUILayout. Height (30 )))
  47. {
  48. MMovie. Stop ();
  49. MAudio. Stop ();
  50. }
  51. // Draw text information
  52. GUILayout. Label ("media name:" + mediaName, GUILayout. Height (30 ));
  53. GUILayout. Label ("Media length:" + mediaLength, GUILayout. Height (30 ));;
  54. }
  55. }


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

Related Article

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.