简体   繁体   English

Unity 4.x:如何在Mecanim中访问AnimationState

[英]Unity 4.x: How to Access AnimationState in Mecanim

I'm using Mecanim Animation System in Unity 4.5.x. 我在Unity 4.5.x中使用Mecanim动画系统。 I have an Animator component on a GameObject called "VictorSprite", with several layers, each layer has several AnimationStates. 我在一个名为“ VictorSprite”的GameObject上有一个Animator组件,该组件有几层,每层都有几个AnimationState。

How can I access each AnimationState to change its properties, specifically speed? 如何访问每个AnimationState更改其属性,特别是速度?

Unfortunately, most of the documents I can find are deprecated, and don't refer to Mecanim. 不幸的是,我可以找到的大多数文档都已弃用,并且没有提到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. Unity 3.x动画组件已弃用,在Unity 4.x中不再推荐使用。

ps I tried this code too, but it doesn't provide good functionality: ps我也尝试了这段代码,但是它没有提供良好的功能:

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. ps 2:基本上,我要在游戏对象的不同层中应用多个动画,并且我希望能够动态地暂停其中的一个。 Setting its weight to 0 is not helpful, because it "resets" the animation, instead of pausing. 将其权重设置为0并没有帮助,因为它“重置”了动画,而不是暂停。

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. 但是,尚不清楚您为什么真正想要这个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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