unity Animator 同时播放两个动画,并动态更换Animator中的AnimationClip

1.。新建分层,将两个AnimationClip放到不同分层中。
unity Animator 同时播放两个动画,并动态更换Animator中的AnimationClip
文章图片

点击右上角设置,调节weight的值(该层动画融合的比重),将此animatorcontroller拖入Animator中,勾选自动运行,运行项目即可同时播放这两个动画。
unity Animator 同时播放两个动画,并动态更换Animator中的AnimationClip
文章图片



2。 动态更换状态机中的clip
思路: 通过AnimatorOverrideController类,动态覆盖状态机中的动画片段

原状态机中有两个动画片段,名字为“1”、“2”,分别在两个分层下
【unity Animator 同时播放两个动画,并动态更换Animator中的AnimationClip】unity Animator 同时播放两个动画,并动态更换Animator中的AnimationClip
文章图片

unity Animator 同时播放两个动画,并动态更换Animator中的AnimationClip
文章图片


将下面的代码挂载到带动画的物体上

public AnimationClip anima1; public AnimationClip anima2; private Animator animator; AnimatorOverrideController overrideController; void Start () {animator = GetComponent(); RuntimeAnimatorController runtimeAnimatorController = animator.runtimeAnimatorController; overrideController = new AnimatorOverrideController(); overrideController.runtimeAnimatorController = runtimeAnimatorController; overrideController["1"] = anima1; overrideController["2"] = anima2; } // Update is called once per frame void Update () {if (Input.GetKeyDown(KeyCode.A)) { animator.runtimeAnimatorController = overrideController; } }

拖入想更换的动画
unity Animator 同时播放两个动画,并动态更换Animator中的AnimationClip
文章图片

运行后,按下键盘A键,就会更换为 34 两个动画片段,并且状态机中的其他逻辑不变
动画“1”为向右移动,“2”为旋转,“3” 为向上移动,“4” 为放大。
unity Animator 同时播放两个动画,并动态更换Animator中的AnimationClip
文章图片


    推荐阅读