视频纹理Unity 5

时间:2015-09-22 12:43:19

标签: unity3d

我有一个问题,让视频纹理显示在团结5.2个人版中。我已经应用了一个带有未点亮着色器的材质并将其指定为视频纹理。我还通过附加到具有视频纹理的对象的脚本来调用特定的视频纹理。

using UnityEngine;
using System.Collections;
[RequireComponent(typeof(AudioSource))]

    public class CallMovie : MonoBehaviour {
        public MovieTexture myMovie;

        // Use this for initialization
        void Start () {
            Debug.Log ("start intro");
            GetComponent<Renderer>().material.mainTexture = myMovie;// get movie
            myMovie.Play ();// shall play movie

        }
        void Update(){
            myMovie.loop =true;
        }
    }

当我按下统一播放按钮时,视频纹理保持黑色,屏幕上没有任何反应,尽管程序说它在使用调试日志检查时运行了视频。

1 个答案:

答案 0 :(得分:0)

由于我无法在您的初始评论中发表评论,以下是尝试用我所知道的答案。

在调试调用之后的第一个语句中,您将实例化材质的maintexture组件设置为myMovie,具体取决于着色器,这可能会或可能不会作为&#39; mainTexture&#39;可能没有引用您期望的纹理。

您可以使用以下方法确保达到所需的纹理

        //Note the diffrence between a material instance and the shared material
        //... dont forget to clean up instances if you make them which hapens when you call .material
        Material instancedMaterial = gameObject.GetComponent<Renderer>().material;
        Material sharedMaterial = gameObject.GetComponent<Renderer>().sharedMaterial;

        //_MainTex is the name of the main texture for most stock shaders ... but not all
        //You can select the shader by clicking the gear in the inspector of the material
        //this will display the shader in the inspector where you can see its properties by name
        instancedMaterial.SetTexture("_MainTex", movie);

以下代码来自我用来设置Unity UI对象RawImage以呈现电影的工作类对象。从我在你的例子中看到的你的电影部分是正确的我怀疑你的问题是使用着色器参数。

using UnityEngine;
using System.Collections;

public class RawImageMovePlayer : MonoBehaviour 
{
    public UnityEngine.UI.RawImage imageSource;

    public bool play;
    public bool isLoop = true;

    public MovieTexture movie;

    // Use this for initialization
    void Start () 
    {
        movie = (MovieTexture)imageSource.texture;
        movie.loop = isLoop;
    }

    // Update is called once per frame
    void Update () 
    {
        if (!movie.isPlaying && play)
            movie.Play();
    }

    public void ChangeMovie(MovieTexture movie)
    {
        imageSource.texture = movie;

        this.movie = (MovieTexture)imageSource.texture;
        this.movie.loop = isLoop;
    }

    public void OnDisable()
    {
        if (movie != null && movie.isPlaying)
            movie.Stop();
    }
}