在Unity编辑器中使用脚本更改动作

4

你好,如何在Unity编辑器中使用脚本更改AnimatorController中的动画?

我想要更改的是红色高亮部分,但需要使用脚本来实现。

输入图片说明

  • 我的使用情况是从一个对象复制动画,处理动画(例如添加偏移旋转),然后添加到另一个对象。由于我的对象有许多子对象,因此需要创建一个脚本来自动化此过程。
1个回答

6

通过编辑器脚本更改 AnimatorController 总是非常棘手的!

  • First of all you need to have your AnimationClip from somewhere

    // somewhere get the AnimationClip from
    var clip = new AnimationClip();
    
  • Then you will have to cast the runtimeAnimatorController to an AnimatorController which is only available in the Editor! (put using UnityEditor; at the top of the script!)

    var controller = (AnimatorController)animator.runtimeAnimatorController;
    
  • Now you can get all its information. In your case you can probably use the default layer (layers[0]) and its stateMachine and according to your image retrieve the defaultState:

    var state = controller.layers[0].stateMachine.defaultState;
    

    or find it using Linq FirstOrdefault (put using System.Linq; at the top of the script) like e.g.

    var state = controller.layers[0].stateMachine.states.FirstOrDefault(s => s.state.name.Equals("SwimmingAnim")).state;
    
    if (state == null)
    {
        Debug.LogError("Couldn't get the state!");
        return;
    }
    
  • Finally assign the AnimationClip to this state using SetStateEffectiveMotion

    controller.SetStateEffectiveMotion(state, clip);
    
请注意,即使您可以使用SetCurve为动画剪辑编写单独的动画曲线,很遗憾它不可能正常地读取它们,因此要实现您想要的功能将会非常困难。

复制一个对象的动画,对其进行处理,例如添加偏移旋转,然后添加到另一个对象上。

您将需要通过AnimationUtility.GetCurveBindings进行复杂的操作 ;)
祝你好运!

是的,这正是我要找的!谢谢 @derHugo。我必须承认这真的很痛苦。我也曾通过AnimationUtility从头开始创建AnimationClip,逐个更改关键帧。 - kkl

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接