用于控制动画器重写控制器的接口。
动画器重写控制器的用途是重写某个控制器的动画剪辑,从而为给定化身定制动画。
在运行时基于相同的 AnimatorController 交换 Animator.runtimeAnimatorController 和 AnimatorOverrideController 不会重置状态机的当前状态。
可以通过以下三种方法使用动画器重写控制器。\
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。 |
GetInstanceID | 返回对象的实例 ID。 |
ToString | 返回对象的名称。 |
Destroy | 移除 GameObject、组件或资源。 |
DestroyImmediate | 立即销毁对象 /obj/。强烈建议您改用 Destroy。 |
DontDestroyOnLoad | 在加载新的 Scene 时,请勿销毁 Object。 |
FindObjectOfType | 返回第一个类型为 type 的已加载的激活对象。 |
FindObjectsOfType | Gets a list of all loaded objects of Type type. |
Instantiate | 克隆 original 对象并返回克隆对象。 |
bool | 该对象是否存在? |
operator != | 比较两个对象是否引用不同的对象。 |
operator == | 比较两个对象引用,判断它们是否引用同一个对象。 |
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.