Search code examples
c#unity-game-enginevideotexture

WWW.Movie not working unity C#


I have been looking at the WWW.movie documentation and i can seam to get it working.

https://docs.unity3d.com/ScriptReference/WWW-movie.html

The code below is attached to a cube containing both the GUI Texture and Audio Source Components. if anyone can help me to get this working i would be very greatful.

I am using unity 5.5.1 and am creating a VR application.

using UnityEngine;
using System.Collections;

public class TouchMovie1 : MonoBehaviour {
 public string url = "file://C:/Users/blobbymatt/VRLibrary/Videos/video.ogv";

 // Use this for initialization
 void Start () {

     StartCoroutine(loadAndPlay ());
 }



 // Update is called once per frame
 void Update () {

 }

 IEnumerator loadAndPlay() {
     // Start download
     var www = new WWW(url);

     // Make sure the movie is ready to start before we start playing
     var movieTexture = www.movie;
     while (!movieTexture.isReadyToPlay) { 
     yield return null;
         Debug.Log("Loading");
     }



     var gt = GetComponent< GUITexture > ();

     // Initialize gui texture to be 1:1 resolution centered on screen
     gt.texture = movieTexture;


     // Assign clip to audio source
     // Sync playback with audio
     var aud = GetComponent< AudioSource > ();
     aud.clip = movieTexture.audioClip;

     // Play both movie & sound
     movieTexture.Play();
     aud.Play();
     yield return null;
 }

}


Solution

  • You shouldn't be using GUITexture because it obsolete. If you want to display video, do it on the RawImage component with GetComponent<RawImage>().texture = yourMovieTexture;.

    If you want to do it on a 3D Model then do it on the MeshRenderer component with GetComponent<MeshRenderer>().material.mainTexture = yourMovieTexture.

    If using Unity 5.6 and above:

    Replace

    www.movie;
    

    with

    www.GetMovieTexture();
    

    There is a new API to play video in Unity 5.6. You can see an example here.

    With the RawImage component, you can do it with the code below:

    using System.Collections;
    using UnityEngine;
    using UnityEngine.UI;
    
    public class TouchMovie1 : MonoBehaviour
    {
        public string url = "http://techslides.com/demos/sample-videos/small.ogv";
        public RawImage videDisplay;
    
        // Use this for initialization
        void Start()
        {
            StartCoroutine(loadAndPlay());
        }
    
        // Update is called once per frame
        void Update()
        {
    
        }
    
        IEnumerator loadAndPlay()
        {
            // Start download
            var www = new WWW(url);
    
            // Make sure the movie is ready to start before we start playing
            var movieTexture = www.movie;
    
            //Assign the Texture to the RawImage
            videDisplay.texture = movieTexture;
    
            while (!movieTexture.isReadyToPlay)
            {
                yield return null;
                Debug.Log("Loading");
            }
    
            // Assign clip to audio source
            // Sync playback with audio
            var aud = GetComponent<AudioSource>();
            aud.clip = movieTexture.audioClip;
    
            // Play both movie & sound
            movieTexture.Play();
            aud.Play();
            yield return null;
        }
    }