I’m trying to play a movie (.mp4) inside my game as intro.
However I’m having a couple of issues I’m running into which I tried to google. But the examples I found do not solve what I’m facing.
First I’m creating the game for both standalone and webplayer.
In the standalone I can load my movie into a movietexture, but what I encounter is that it starts with a black frame which I do not have in my actual movie. And besides that, it doesn’t play straight from the beginning.
For example, my movie starts with the black frame and jumps 3/4 seconds into the movie and starts from there.
I’ve tried a couple of solution with coroutines and without, but I can’t seam to solve this.
Besides that. I also try to publish this game project for the webplayer and I can’t seam to stream the video from a folder named “movie” where the movie named “movie_beheer.mp4” is located. The html file is outside the movie folder. So it should find it somehow, but doesn’t stream at all.
Also I’m trying to find a way how to check if the movie is finished playing. However if I use !movieTexture.isPlaying and load a scene, it just doesn’t stream the video in webplayer but just loads the scene and acts like it’s done playing the movie.
Here is what I got so far with C#. I just can’t find a way to have the video play correctly in both situations.
using UnityEngine;
using System.Collections;
// [ multi platform support ]
public enum MoviePlayTextureState
{
WEBPLAYER,
STANDALONE,
}
// [ Required Components ]
[RequireComponent(typeof(AudioSource))]
public class MoviePlayTexture : MonoBehaviour
{
// [ Variables ]
public MovieTexture movieTexture;
public string pathMovieForWeb; // path name is string: "movie/movie_beheer.mp4"
MoviePlayTextureState state;
// [ Functions ]
/*----------------------------------------------------------------------
Start ( Unity standard )
----------------------------------------------------------------------*/
void Start()
{
detectPlatform();
switch (state)
{
case MoviePlayTextureState.WEBPLAYER:
loadMovieWeb(pathMovieForWeb);
break;
case MoviePlayTextureState.STANDALONE:
loadMovieStandalone(movieTexture);
break;
default:
break;
}
}
/*----------------------------------------------------------------------
Update ( Unity standard )
----------------------------------------------------------------------*/
void Update()
{
playMovie();
}
/*----------------------------------------------------------------------
detectPlatform
----------------------------------------------------------------------*/
void detectPlatform()
{
bool platform = Application.isWebPlayer;
// - check platform
switch(platform)
{
case true:
state = MoviePlayTextureState.WEBPLAYER;
break;
case false:
state = MoviePlayTextureState.STANDALONE;
break;
default:
break;
}
}
/*----------------------------------------------------------------------
loadMovieWeb
----------------------------------------------------------------------*/
void loadMovieWeb(string vFilePath)
{
WWW www = new WWW(vFilePath);
movieTexture = (MovieTexture)www.movie;
initMovie();
}
/*----------------------------------------------------------------------
loadMovieStandalone
----------------------------------------------------------------------*/
void loadMovieStandalone(MovieTexture vMovieTexture)
{
initMovie();
}
/*----------------------------------------------------------------------
initMovie
----------------------------------------------------------------------*/
void initMovie()
{
GUITexture tex = this.guiTexture;
float aspectRatio = Screen.width * ((movieTexture.height + 0.0f) / (movieTexture.width + 0.0f));
tex.pixelInset = new Rect(0 - Mathf.Ceil(Screen.width * 0.5f), 0 - Mathf.Ceil(aspectRatio * 0.5f), Screen.width, aspectRatio);
tex.texture = movieTexture;
audio.clip = movieTexture.audioClip;
audio.minDistance = 1000;
}
/*----------------------------------------------------------------------
playMovie
----------------------------------------------------------------------*/
void playMovie()
{
if (movieTexture.isReadyToPlay !movieTexture.isPlaying)
{
movieTexture.Play();
audio.Play();
}
}
}