首页 > 其他分享 >AnimatorOverrideController使用

AnimatorOverrideController使用

时间:2022-11-09 01:11:08浏览次数:74  
标签:ctrl runtimeAnimatorController 使用 AnimatorOverrideController curColorAnimClipInd

# 需要用到这几个文件

(1) 状态机动画文件:可以是一套真实的动画,也可以是一套占位动画,这边使用了占位动画:ColorAnim_Placeholder.anim, ScaleAnim_Placeholder.anim, PosAnim_Placeholder.anim

(2) 通用状态机文件:Common.controller

(3) controller状态机动画替换文件:Common_Overrde.overrideController

(4) 实际动画文件

下面的代码按1键,用下一个动画替换掉状态机的ColorAnim_Placehoder,这边就是在红和蓝颜色动画间切换

using UnityEditor.Animations;
using UnityEngine;

public class AnimOverrideCtrlDemo : MonoBehaviour
{

    public AnimationClip[] colorAnimClips;

    private AnimatorOverrideController _ctrl;

    private int _curColorAnimClipIndex;
    private AnimationClipOverridesList _clipOverridesList;

    void Start()
    {
        var animator = GetComponent<Animator>();
        if (animator.runtimeAnimatorController is AnimatorOverrideController) //使用.overrideController文件
        {
            _ctrl = (AnimatorOverrideController)animator.runtimeAnimatorController;
        }
        else if (animator.runtimeAnimatorController is AnimatorController) //代码方式创建AnimatorOverrideController
        {
            _ctrl = new AnimatorOverrideController(animator.runtimeAnimatorController);
            animator.runtimeAnimatorController = _ctrl;
        }
    }

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Alpha1)) //next color
        {
            var clip = colorAnimClips[_curColorAnimClipIndex];
            if (null != clip)
                _ctrl["ColorAnim_Placeholder"] = clip;

            _curColorAnimClipIndex++;
            if (_curColorAnimClipIndex >= colorAnimClips.Length)
                _curColorAnimClipIndex = 0;
        }
    }

}

效果

 

# .controller文件和.overrideController文件图标的区别

 

# GetOverrides和ApplyOverrides函数的使用

[System.Serializable]
public class MultiAnimClip
{
    public AnimationClip colorAnim;
    public AnimationClip scaleAnim;
    public AnimationClip posAnim;
}


public class AnimationClipOverridesList : List<KeyValuePair<AnimationClip, AnimationClip>>
{
    public AnimationClipOverridesList(int capacity) : base(capacity) { }

    public AnimationClip this[string name]
    {
        get { return this.Find(x => x.Key.name.Equals(name)).Value; }
        set
        {
            int index = this.FindIndex(x => x.Key.name.Equals(name));
            if (index != -1)
                this[index] = new KeyValuePair<AnimationClip, AnimationClip>(this[index].Key, value);
        }
    }
}

 

using System.Collections.Generic;
using UnityEditor.Animations;
using UnityEngine;

public class AnimOverrideCtrlDemo2 : MonoBehaviour
{
    public MultiAnimClip[] multiAnimClips; //用于替换的实际动画

    private AnimatorOverrideController _ctrl;

    private int _curColorAnimClipIndex;
    private AnimationClipOverridesList _clipOverridesList;

    void Start()
    {
        var animator = GetComponent<Animator>();
        if (animator.runtimeAnimatorController is AnimatorOverrideController) //使用.overrideController文件
        {
            _ctrl = (AnimatorOverrideController)animator.runtimeAnimatorController;
        }
        else if (animator.runtimeAnimatorController is AnimatorController) //代码方式创建AnimatorOverrideController
        {
            _ctrl = new AnimatorOverrideController(animator.runtimeAnimatorController);
            animator.runtimeAnimatorController = _ctrl;
        }

        _clipOverridesList = new AnimationClipOverridesList(_ctrl.overridesCount);
        _ctrl.GetOverrides(_clipOverridesList);
    }

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Alpha1)) //next color
        {
            var clips = multiAnimClips[_curColorAnimClipIndex];
            if (null != clips)
            {
                _clipOverridesList["ColorAnim_Placeholder"] = clips.colorAnim;
                _clipOverridesList["ScaleAnim_Placeholder"] = clips.scaleAnim;
                _clipOverridesList["PosAnim_Placeholder"] = clips.posAnim;
                _ctrl.ApplyOverrides(_clipOverridesList);
            }

            _curColorAnimClipIndex++;
            if (_curColorAnimClipIndex >= multiAnimClips.Length)
                _curColorAnimClipIndex = 0;
        }
    }

}

 

【参考】

Unity - Scripting API: AnimatorOverrideController (unity3d.com)

unity Animator Override Controller的使用_BlueBones_fan的博客-CSDN博客_animatoroverridecontroller

Animator Override Controller学习及性能测试 - 爱折腾的西山居士 - 博客园 (cnblogs.com)

使用AnimatorControllerOverride的坑_chqj_163的博客-CSDN博客

使用AnimatorOverrideController动态更换animationclip注意事项_不会程序的美术的博客-CSDN博客

 

标签:ctrl,runtimeAnimatorController,使用,AnimatorOverrideController,curColorAnimClipInd
From: https://www.cnblogs.com/sailJs/p/15192507.html

相关文章