简体   繁体   中英

Unity 4.x: How to Access AnimationState in Mecanim

I'm using Mecanim Animation System in Unity 4.5.x. I have an Animator component on a GameObject called "VictorSprite", with several layers, each layer has several AnimationStates.

How can I access each AnimationState to change its properties, specifically speed?

Unfortunately, most of the documents I can find are deprecated, and don't refer to Mecanim. I have also tried this code:

AnimationState animState = GameObject.Find("VictorSprite").GetComponent<Animator>().animation["VictorTalk"]; 

But, I receive this error message:

"MissingComponentException: There is no 'Animation' attached to the "VictorSprite" game object, but a script is trying to access it. You probably need to add a Animation to the game object "VictorSprite". Or your script needs to check if the component is attached before using it."

Unity 3.x Animation component is deprecated and is not recommended anymore in Unity 4.x.

ps I tried this code too, but it doesn't provide good functionality:

AnimatorStateInfo animState = GameObject.Find("VictorSprite").GetComponent<Animator>().GetCurrentAnimatorStateInfo(2);

ps 2: Basically, I'm applying multiple animations in different layers on a game object, and I want to be able to pause one of them dynamically. Setting its weight to 0 is not helpful, because it "resets" the animation, instead of pausing.

To speed up,

    var animator = unityChan.GetComponent<Animator> ();
    animator.speed = 10.0f;

This should give you what you want.

Animator animator = GameObject.Find("ObjectInScene").GetComponent<Animator>();
AnimationInfo[] stateInfo = animator.GetCurrentAnimationClipState(index);

However, it is a bit unclear why you would actually want this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM