RabidTunes
RabidTunes

Reputation: 964

How to change Movie Textures in runtime in Unity Pro?

I have a couple of videos in Unity Pro which I can play through an object and a MovieTexture. Through the inspector I manually set the MovieTexture to be one of the .ogg videos I have and it plays successfully.

What I want to do is to change the Movie Texture in runtime, through code, when certain conditions are met (for the sake of simplicity, something like when you press the space bar the video currently playing changes to another video).

What currently works for me but using the inspector is this:

MovieTexture movTex;
AudioSource movAudio;

void Start()
{
    // I have set the movie manually through the inspector by drag and drop
    // onto the texture component
    movTex = (MovieTexture)GetComponent<Renderer>().material.mainTexture;
    movAudio = GetComponent<AudioSource>();
    movAudio.clip = movTex.audioClip;
    movTex.Play(); // Autoplay on start
    movAudio.Play();
    movTex.loop = true; // Loop forever
    ...
}

And I have already tried the code that I found on this post, putting of course my desired videos under Resources folder, but it doesn't work. A plain white texture with no audio is what I'm getting.

This is the code I'm trying to make to work but I can't:

MovieTexture movTex;
AudioSource movAudio;

void Start()
{
    movTex = (MovieTexture) Resources.Load( "V00001" , typeof( MovieTexture ) );
    transform.renderer.material.mainTexture = movie;
    transform.audio.clip = movie.audioClip;
    movAudio = GetComponent<AudioSource>();
    movAudio.clip = movTex.audioClip;
    movTex.Play(); // Autoplay on start
    movAudio.Play();
    movTex.loop = true; // Loop forever
    ...
}

void Update()
{
    if("Spacebar is pressed"){
        movTex = (MovieTexture) Resources.Load( "V00001" , typeof( MovieTexture ) );
        movAudio = GetComponent<AudioSource>();
        movAudio.clip = movTex.audioClip;
        movTex.Play(); // Autoplay on start
        movAudio.Play();
        movTex.loop = true; // Loop forever
    }
    ...
}

What am I doing wrong?

Upvotes: 0

Views: 1415

Answers (1)

FreeFly
FreeFly

Reputation: 153

movieTexture are essentially Texture like the others and they can be applied like every other texture. However, what you need to keep in mind is that, in order to load something that is not in your scene, usually you have to take it from a Resources folder.

Let's assume you have a movie file in your Resources folder, somewhere in your assets folder, you'd only have to Load id by writing

MovieTexture movie = (MovieTexture) Resources.Load( "fileName" , typeof( MovieTexture ) );

After that, you can apply it on your object's material texture by writing

if(yourConditions){
    myGameObject.GetComponent<MeshRenderer>().material.mainTexture = movie;
}    

}

A complete function can look like this:

void Start()
{
    MovieTexture movie = (MovieTexture) Resources.Load( "fileName" , typeof( MovieTexture ) );
    if(movie != null){
        this.GetComponent<MeshRenderer>().material.mainTexture = movie;
        // making sure there is an audio source attached
        if(this.GetComponent<AudioSource>() == null){
            this.AddComponent<AudioSource>();
        }
        AudioSource aud = GetComponent<AudioSource>();
        aud.clip = movie.audioClip;
        movie.Play(); // Autoplay on start
        aud.Play();     
    }

}

Upvotes: 1

Related Questions