WWW.Movie unity C# 不工作

WWW.Movie not working unity C#

我一直在查看 WWW.movie 文档,我可以接缝让它工作。

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

下面的代码附加到包含 GUI 纹理和音频源组件的立方体。如果有人可以帮助我完成这项工作,我将非常感激。

我正在使用 unity 5.5.1 并正在创建 VR 应用程序。

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;
 }

}

您不应该使用 GUITexture,因为它已过时。如果要显示视频,请在 RawImage 组件上使用 GetComponent<RawImage>().texture = yourMovieTexture; 进行。

如果您想在 3D 模型上执行此操作,请使用 GetComponent<MeshRenderer>().material.mainTexture = yourMovieTextureMeshRenderer 组件上执行此操作。

如果使用 Unity 5.6 及以上版本:

替换

www.movie;

www.GetMovieTexture();

Unity 5.6 中有一个新的 API 可以播放视频。你可以看一个例子 .

使用 RawImage 组件,您可以使用以下代码完成:

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;
    }
}