Version: 2022.1

AnimatorOverrideController

class in UnityEngine

/

继承自:RuntimeAnimatorController

切换到手册

描述

用于控制动画器重写控制器的接口。

动画器重写控制器的用途是重写某个控制器的动画剪辑,从而为给定化身定制动画。 在运行时基于相同的 AnimatorController 交换 Animator.runtimeAnimatorControllerAnimatorOverrideController 不会重置状态机的当前状态。

可以通过以下三种方法使用动画器重写控制器。\ 1.在 Editor 中创建动画器重写控制器。\ 2.在运行时每帧更改一个动画剪辑(基本用例)。\ 在这种情况下,可以使用索引器运算符 AnimatorOverrideController.this[string],但要小心,因为每次调用都会触发动画器剪辑绑定的重新分配。

using UnityEngine;

public class SwapWeapon : MonoBehaviour { public AnimationClip[] weaponAnimationClip;

protected Animator animator; protected AnimatorOverrideController animatorOverrideController;

protected int weaponIndex;

public void Start() { animator = GetComponent<Animator>(); weaponIndex = 0;

animatorOverrideController = new AnimatorOverrideController(animator.runtimeAnimatorController); animator.runtimeAnimatorController = animatorOverrideController; }

public void Update() { if (Input.GetButtonDown("NextWeapon")) { weaponIndex = (weaponIndex + 1) % weaponAnimationClip.Length; animatorOverrideController["shot"] = weaponAnimationClip[weaponIndex]; } } }

3.在运行时每帧更改诸多动画剪辑(高级用例)。\ AnimatorOverrideController.ApplyOverrides 方法非常适合这种情况,因为它能够将动画器剪辑绑定重新分配的数量减少到每次调用只有一个。

using UnityEngine;
using System.Collections.Generic;

public class AnimationClipOverrides : List<KeyValuePair<AnimationClip, AnimationClip>> { public AnimationClipOverrides(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); } } }

public class Weapon { public AnimationClip singleAttack; public AnimationClip comboAttack; public AnimationClip dashAttack; public AnimationClip smashAttack; }

public class SwapWeapon : MonoBehaviour { public Weapon[] weapons;

protected Animator animator; protected AnimatorOverrideController animatorOverrideController;

protected int weaponIndex;

protected AnimationClipOverrides clipOverrides; public void Start() { animator = GetComponent<Animator>(); weaponIndex = 0;

animatorOverrideController = new AnimatorOverrideController(animator.runtimeAnimatorController); animator.runtimeAnimatorController = animatorOverrideController;

clipOverrides = new AnimationClipOverrides(animatorOverrideController.overridesCount); animatorOverrideController.GetOverrides(clipOverrides); }

public void Update() { if (Input.GetButtonDown("NextWeapon")) { weaponIndex = (weaponIndex + 1) % weapons.Length; clipOverrides["SingleAttack"] = weapons[weaponIndex].singleAttack; clipOverrides["ComboAttack"] = weapons[weaponIndex].comboAttack; clipOverrides["DashAttack"] = weapons[weaponIndex].dashAttack; clipOverrides["SmashAttack"] = weapons[weaponIndex].smashAttack; animatorOverrideController.ApplyOverrides(clipOverrides); } } }

变量

overridesCount返回重写数。
runtimeAnimatorController动画器重写控制器重写的运行时动画控制器。
this[string]返回重写动画剪辑(如果已设置)或原有动画剪辑命名的名称。

构造函数

AnimatorOverrideController创建一个空的动画器重写控制器。

公共函数

ApplyOverrides对该动画器重写控制器应用重写列表。
GetOverrides获取该动画器重写控制器中当前定义的动画剪辑重写的列表。

继承的成员

变量

hideFlags该对象应该隐藏、随场景一起保存还是由用户修改?
name对象的名称。
animationClips检索控制器使用的所有 AnimationClip。

公共函数

GetInstanceIDGets the instance ID of the object.
ToString返回对象的名称。

静态函数

Destroy移除 GameObject、组件或资源。
DestroyImmediate立即销毁对象 /obj/。强烈建议您改用 Destroy。
DontDestroyOnLoad在加载新的 Scene 时,请勿销毁 Object。
FindObjectOfType返回第一个类型为 type 的已加载的激活对象。
FindObjectsOfTypeGets a list of all loaded objects of Type type.
Instantiate克隆 original 对象并返回克隆对象。

运算符

bool该对象是否存在?
operator !=比较两个对象是否引用不同的对象。
operator ==比较两个对象引用,判断它们是否引用同一个对象。