http://www.ceeger.com/forum/read.php?tid=19138
public AnimationClip clip; Animator anim; void Awake() { anim = GetComponent<Animator>(); } void Start () { AnimatorOverrideController overrideController = new AnimatorOverrideController(); overrideController.runtimeAnimatorController = anim.runtimeAnimatorController; overrideController["testCip"] = clip; anim.runtimeAnimatorController = overrideController; }
这里一定要注意, overrideController["testCip"] = clip;里面的testClip不是状态机里面的状态名,而是实际要替换的clip的名字.....
也可以用AnimationClipPair得到clip的名字
RuntimeAnimatorController tempController = anim.runtimeAnimatorController; AnimatorOverrideController overrideController = new AnimatorOverrideController(); overrideController.runtimeAnimatorController = tempController; overrideController.PerformOverrideClipListCleanup();//<span style="color:#ff0000;">这个函数在U5.3和5.0都不存在应该是去掉了吧</span> overrideController[overrideController.clips[0].originalClip.name] = clip; overrideController[overrideController.clips[1].originalClip.name] = clip2; foreach (AnimationClipPair pairs in overrideController.clips) { Debug.Log(pairs.originalClip.name ); if (pairs.overrideClip!=null) { Debug.Log(pairs.overrideClip.name ); } } anim.runtimeAnimatorController = overrideController;