enumeration
Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.
CloseFor some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.
CloseOptions that choose which animations to stop when playing an animation.
Methods use this enum to choose which animations to stop based on the layer or component of the started animation. For example, the Animation.Play method uses StopSameLayer
to stop animations on the same layer as the started animation. This method uses StopAll
to stop animations associated with the same Animation component as the started animation.
Additional resources: Animation.CrossFade, Animation.CrossFadeQueued, Animation.Play, Animation.PlayQueued.
// The following example demonstrates animation layers and how to use the PlayMode enum to animate a collectable coin. // The coin has four animation clips: // 1. Spin: A looping animation where the coin rotates on the y-axis. // 2. Slide: A looping animation where the coin moves back and forth. // 3. Bop: The coin moves upwards. // 4. Poof: The coin disappears with an effect. // The coin behaves as follows: // 1. The coin slides while spinning. // 2. If collected by the player, the coin moves upwards while spinning. // 3. If missed by the player, the coin stops spinning and disappears with an effect. using System.Collections; using UnityEngine; [RequireComponent(typeof(Animation))] public class PlayModeExample : MonoBehaviour { // Determines whether the coin was successfully collected. // True plays the coin collected animation. False plays the coin missed animation. public bool collectCoin; public float waitTime = 0.8f; // Length of the timer used for testing the coin collected and coin missed animations. Animation m_AnimComponent; void Start() { m_AnimComponent = GetComponent<Animation>(); // Slide and Bop need to be played in parallel to Spin, so they are set on different layers. // Spin is on the default layer which is 0. m_AnimComponent["Slide"].layer = 1; m_AnimComponent["Bop"].layer = 1; PlayCoinSlideAnimations(); // Ideally, PlayCoinCollectedAnimation() should be called when the player collides with the coin // and PlayCoinMissedAnimation() should be called at the end of the coin's lifetime, but // this would make the example too long. Instead, this coroutine provides a quick test while // Spin and Slide are playing. StartCoroutine(CollectOrHideCoinAfterDelay(waitTime)); } void PlayCoinSlideAnimations() { // Since no value for PlayMode is passed, PlayMode.StopSameLayer is used by default. m_AnimComponent.Play("Spin"); // Slide needs to be played concurrently to Spin, so PlayMode.StopSameLayer is used instead of PlayMode.StopAll. // StopAll would have stopped the Spin animation. m_AnimComponent.Play("Slide"); } IEnumerator CollectOrHideCoinAfterDelay(float time) { yield return new WaitForSeconds(time); if(collectCoin) PlayCoinCollectedAnimation(); else PlayCoinMissedAnimation(); } void PlayCoinCollectedAnimation() { // Since Bop is on the same layer as Slide, it stops Slide but not Spin since PlayMode.StopSameLayer is implicitly used here. m_AnimComponent.Play("Bop"); } void PlayCoinMissedAnimation() { // Since PlayMode.StopAll is used here, both Spin and Slide stop and only Poof plays. m_AnimComponent.Play("Poof", PlayMode.StopAll); } }
StopSameLayer | Stops animations on the same layer as the started animation. This is the default behavior. |
StopAll | Stops animations played by the same component as the started animation. |
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.