- Home /
End of Animation
Hello, I have this animation right, and when it finishes I want it to load a scene. So for example I have my camera... when a certain button is clicked, the camera moves behind the wall where you only see black. Now when the animation on the camera finishes I would like it to send me to, for example "Scene X".
Thanks!
Answer by dannyskim · Feb 13, 2013 at 05:31 PM
Here are some animation extensions that I wrote for a project to do basically what you're asking, as well as things as callbacks for OnUpdate, a delay before the animation is played, etc.
using UnityEngine;
using System;
using System.Collections;
/* Animation Extensions, Written by DSK
*
* Used to extend functionality of the Unity Animation Class by way of CoRoutine
*
*/
public static class AnimationExtensions {
public static IEnumerator PlayWithOptions(this Animation animation, string animName, Action onComplete)
{
animation.Play(animName);
while (animation.isPlaying) { yield return null; }
onComplete();
}
public static IEnumerator PlayWithOptions(this Animation animation, string animName, float delay, Action onComplete)
{
yield return new WaitForSeconds(delay);
animation.Play(animName);
while (animation.isPlaying) { yield return null; }
onComplete();
}
public static IEnumerator PlayWithOptions(this Animation animation, string animName, Action onUpdate, Action onComplete)
{
animation.Play(animName);
while (animation.isPlaying)
{
onUpdate();
yield return null;
}
onComplete();
}
public static IEnumerator PlayWithOptions(this Animation animation, string animName, float delay, Action onUpdate, Action onComplete)
{
yield return new WaitForSeconds(delay);
animation.Play(animName);
while (animation.isPlaying)
{
onUpdate();
yield return null;
}
onComplete();
}
}
These functions are extending the Unity Animation class. For use, you would do something like:
StartCoroutine( gameObject.animation.PlayWithOptions(
animToPlay,
() =>
{
Debug.Log( "ANIMATION FINISHED, LOADING SCENE X" );
Application.LoadLevel( "SceneX" );
}
)
);
the whole thing does what you need... of course if you copy-paste it, you probably won't understand why it works. btw nice script
Yes, the second one does what you need, but you need the first one to accomplish it. Copy and paste the first code section and save it as a C# script in your project, then you'll be able to call the code as depicted in the second code section.
@z_murc, thanks for the kind comments.
The animation I'm calling it fade_into_black should go into the animname?