Unity3d playing video

Source: Internet
Author: User

http://blog.csdn.net/awnuxcvbn/article/details/17618919

Click here to download the video converter, the converter will convert other formats into OGV format, directly recognized by unity, do not need to install QuickTime do not need to restart, more importantly than the MOV into unity to be clear.

[CSharp]View PlainCopy 
  1. Using Unityengine;
  2. Using System.Collections;
  3. Public class Movietest:monobehaviour
  4. {
  5. //Movie Textures
  6. Public movietexture Movtexture;
  7. void Start ()
  8. {
  9. //Set the main texture of the current object as the movie texture
  10. Renderer.material.mainTexture = movtexture;
  11. //Set movie texture playback mode to loop
  12. Movtexture.loop = true;
  13. //startcoroutine (Downloadmovie ());
  14. }
  15. void Ongui ()
  16. {
  17. if (Guilayout.button ("play/Continue"))
  18. {
  19. / /play/continue playing video
  20. if (!movtexture.isplaying)
  21. {
  22. Movtexture.play ();
  23. }
  24. }
  25. if (Guilayout.button ("pause playback"))
  26. {
  27. //Pause playback
  28. Movtexture.pause ();
  29. }
  30. if (Guilayout.button ("Stop playing"))
  31. {
  32. //Stop playing
  33. Movtexture.stop ();
  34. }
  35. }
  36. IEnumerator Downloadmovie ()
  37. {
  38. www www = new www ("http://127.0.0.1/Head.ogv");
  39. Yield return www;
  40. Debug.Log (Time.time);
  41. Movtexture = Www.movie;
  42. //Set the main texture of the current object as the movie texture
  43. Renderer.material.mainTexture = movtexture;
  44. Movtexture.loop = true;
  45. }
  46. }

Plus sound

[CSharp]View PlainCopy 
  1. Using Unityengine;
  2. Using System.Collections;
  3. Public class Movietest:monobehaviour
  4. {
  5. //Movie Textures
  6. Public movietexture Movtexture;
  7. //Sound
  8. Public Audiosource Movaudio;
  9. void Start ()
  10. {
  11. //Set the main texture of the current object as the movie texture
  12. Renderer.material.mainTexture = movtexture;
  13. //Set movie texture playback mode to loop
  14. Movtexture.loop = true;
  15. //startcoroutine (Downloadmovie ());
  16. }
  17. void Ongui ()
  18. {
  19. if (Guilayout.button ("play/Continue"))
  20. {
  21. / /play/continue playing video
  22. if (!movtexture.isplaying)
  23. {
  24. Movtexture.play ();
  25. Movaudio.play ();
  26. }
  27. }
  28. if (Guilayout.button ("pause playback"))
  29. {
  30. //Pause playback
  31. Movtexture.pause ();
  32. Movaudio.pause ();
  33. }
  34. if (Guilayout.button ("Stop playing"))
  35. {
  36. //Stop playing
  37. Movtexture.stop ();
  38. Movaudio.stop ();
  39. }
  40. }
  41. IEnumerator Downloadmovie ()
  42. {
  43. www www = new www ("http://127.0.0.1/Head.ogv");
  44. Yield return www;
  45. Debug.Log (Time.time);
  46. Movtexture = Www.movie;
  47. //Set the main texture of the current object as the movie texture
  48. Renderer.material.mainTexture = movtexture;
  49. Movtexture.loop = true;
  50. }
  51. }


Note: 1280*720 resolution of video played in this way does not produce black edges.

Previously just testing, no actual use, found that multiple clicks to play will have a memory leak. The following method solves

[CSharp]View PlainCopy 
  1. Using Unityengine;
  2. Using System.Collections;
  3. Public class Webmovie:monobehaviour {
  4. //Movie Textures
  5. Public movietexture Movtexture;
  6. www www;
  7. void Start ()
  8. {
  9. }
  10. void Ongui ()
  11. {
  12. if (Guilayout.button ("load Video"))
  13. {
  14. Startcoroutine (Downloadmovie ());
  15. }
  16. if (Guilayout.button ("play/Continue"))
  17. {
  18. / /play/continue playing video
  19. if (!movtexture.isplaying)
  20. {
  21. Movtexture.play ();
  22. }
  23. }
  24. if (Guilayout.button ("pause playback"))
  25. {
  26. //Pause playback
  27. Movtexture.pause ();
  28. }
  29. if (Guilayout.button ("Stop playing"))
  30. {
  31. //Stop playing
  32. Movtexture.stop ();
  33. }
  34. if (Guilayout.button ("Unload Memory"))
  35. {
  36. Renderer.material.mainTexture = null;
  37. Movtexture = null;
  38. www = null;
  39. Resources.unloadunusedassets ();
  40. }
  41. }
  42. IEnumerator Downloadmovie ()
  43. {
  44. www = new www ("http://127.0.0.1/mumaren.ogv");
  45. Yield return www;
  46. Debug.Log (Time.time);
  47. Movtexture = Www.movie;
  48. //Set the main texture of the current object as the movie texture
  49. Renderer.material.mainTexture = movtexture;
  50. Movtexture.loop = true;
  51. }
  52. }


is to destroy the WWW object, but before this, it is necessary to remove other objects to the WWW reference, such as the material ball of the main map, the declaration of the movtexture, and perhaps AudioClip.

Unity3d playing video

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.