0
\$\begingroup\$

I see that there's a way to get each animation clip like the following, but is there a way to get all of the animations in a given project and play/stop them at the same time?

public class Anim: MonoBehaviour {

protected Animation anim;

// Use this for initialization
void Start () {

    anim = GetComponent<Animation>();

}

void PlayAnimation
{
    if ()//some sort of condition 
    {
        anim.Play ("Roll Dice");
    }
    else {
        anim.Stop();
    }
}
\$\endgroup\$
  • \$\begingroup\$ You would have to write some kind of animation controller. \$\endgroup\$ – jgallant Sep 22 '16 at 15:35
  • \$\begingroup\$ Please visit this page to have your accounts merged, this will allow you to comment on your questions and their answers. \$\endgroup\$ – Vaillancourt Sep 22 '16 at 16:40
1
\$\begingroup\$

What you are looking for is :
https://docs.unity3d.com/ScriptReference/Object.FindObjectsOfType.html

public static Object[] FindObjectsOfType(Type type);

Returns a list of all active loaded objects of Type type. It will return no assets (meshes, textures, prefabs, ...) or inactive objects.

or its generic version (more prefered)

public static T[] FindObjectsOfType();

And to get all the objects of type Animation and stop the running animations you will do:

void StopAllAnimation() 
{
    var allAnims = FindObjectsOfType<Animation>();
    foreach( var anim in allAnims ) 
    {
        anim.Stop();
    }
}

p.s: this function will return list of all objects of given type that are present in current scene, no matter where you call the function or which game object the animation object belongs to

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.