通过编辑器脚本更改 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
进行复杂的操作 ;)
AnimationUtility
从头开始创建AnimationClip
,逐个更改关键帧。 - kkl