- Home /
On movie end
Hi Guys,
I am having an issue with triggering an event when a movie completes. I am using the isPlaying property of the movie texture to check however it is never set to true.
GUI.DrawTexture(Rect(0, 0, Screen.width, Screen.height), Intro);
Intro.Play();
if(Intro.isPlaying == false) {
GUI.Label(Rect(25, Screen.height / 2, 500, 500), "Here is some Content", IntroText);
Debug.Log("Video NOT Playing");
} else {
Debug.Log("Video Playing");
}
Any Ideas on how I can trigger an event when the movie ends?
Thanks.
Sorry to bump this but dose anyone have an answer? I am on a deadline
Answer by Fattie · Jul 05, 2015 at 08:52 AM
Simply loop on isPlaying...
public void PlayThenDoSomething(Action callback)
{
mt.Stop();
mt.Play();
StartCoroutine(FindEnd(callback));
}
private IEnumerator FindEnd(Action callback)
{
while(mt.isPlaying)
{
yield return 0;
}
callback();
yield break;
}
Answer by alexfeature · Feb 08, 2012 at 07:10 PM
Hi Tex,
Here is something that may help you.
void PlayMyClip()
{
if(!Intro.isPlaying)
{
// Play clip
Intro.Play();
// Wait for the clip to finish
StartCoroutine(Wait(_currentClip.duration));
//
// Add your code here
//
}
}
private IEnumerator Wait(float duration)
{
yield return new WaitForSeconds(duration);
}
The idea is pretty simple. First you trigger the clip to play and then wait for it to finish using yield. Once it finishes playing you can carry on with your other stuff.
You can of course send a message after the wait function call or do something a little more advanced (read complicated :) ) depending on your actual requirements.
Hope this helps.
You skip ahead and everything goes out of sync. Simple :)
Your code doesn't work. You need to wait your coroutine, so Play$$anonymous$$yClip must be a coroutine itself.
IEnumerator Play$$anonymous$$yClip()
{
if(!Intro.isPlaying)
{
// Play clip
Intro.Play();
// Wait for the clip to finish
yield return StartCoroutine(Wait(_currentClip.duration)); // at this point, you can use new WaitForSeconds here, directly
//
// Add your code here
//
}
}
private IEnumerator Wait(float duration)
{
yield return new WaitForSeconds(duration);
}
There is a second option (I don't like it) : using callbacks
void Play$$anonymous$$yClip()
{
if(!Intro.isPlaying)
{
// Play clip
Intro.Play();
// Wait for the clip to finish
StartCoroutine(Wait(_currentClip.duration, OnWaitFinished));
}
}
private void OnWaitFinished()
{
//
// Add your code here
//
}
private IEnumerator Wait(float duration, System.Action callback)
{
yield return new WaitForSeconds(duration);
if(callback != null) callback();
}
Note that this kind of StartCoroutine * Wait function already exists on Unity : Invoke. http://docs.unity3d.com/es/current/ScriptReference/$$anonymous$$onoBehaviour.Invoke.html
Your answer
Follow this Question
Related Questions
Movie Texture 0 Answers
www.movie.other formats 1 Answer
MovieTexture stops playing after first frame 0 Answers
Movie Texture and EndUser's PC 0 Answers
problem about hiccups during play video? 0 Answers