Unity流水账4:Animation API(二)

十四、Animator
1.描述
  控制Mecanim animation系统的接口
2.Properties

angularVelocity:获得上一帧计算的avatar的角速度
函数定义:public Vector3 angularVeclocity;
applyRootMotion:是否应用root motion。root motion是对象的整个mesh远离其起点移动的效果,但该运动是由动画本身创建的,而不是通过更改transform位置。注意,当脚本实现MonoBehaviour.OnAnimatorMove函数时,applyRootMotion无效。在运行时更改applyRootMotion的值将重新初始化animator.
函数定义:public bool applyRootMotion;
avatar:获取/设置当前Avatar。不完全支持在运行时更改avatar.如果使用它,需要你自担风险。
函数定义:public Avatar avatar;
bodyPosition:身体中心的位置。位置在世界空间。你只应在OnAnimatorIK()函数调用中设置此值。
函数定义:public Vector3 bodyPosition;
bodyRotation:身体中心的旋转。旋转在世界空间。你只应在OnAnimatorIK()函数调用中设置此值。
函数定义:public Quaternion bodyRotation;
cullingMode:控制剔除此Animator组件。
函数定义:public AnimatorCullingMode cullingMode.
deltaPosition:获取上一帧的avatar的delta position。
函数定义:public Vector3 deltaPosition;
deltaRotation:获取上一帧的avatar的delta rotation
函数定义:public Quaternion deltaRotation
feetPivotActive:在身体质心和脚的pivot之间混合pivot point。在0%时,blending point是身体质心。在100%时,blending point是脚的pivot。
函数定义:public float feetPivotActive
fireEvent:设置Animator是否发送AnimationEvent类型的事件。默认值是true
函数定义:public bool fireEvents
gravityWeight:当前重力权重基于当前播放的动画。
函数定义:public float gravityWeight;
hasBoundPlayables:如果Animator分配了任何playables,则返回true.
函数定义:public bool hasBoundPlayables;
hasRootMotion:如果当前的rig具有root motion,则返回true。
函数定义:public bool hasRootMotion;
hasTransformHierarchy:如果对象具有transform hierarchy,则返回true。基于Model Importer的Optmize Game Object切换。
函数定义:public bool hasTransformHierarchy;
humanScale:返回humanoid rig的当前avatar的比例(默认情况下,如果rig是通用的,则为1);比例相对于Unity的默认Avatar.
函数定义:public float humanScale
isHuman:如果当前rig是humanoid,则返回true,如果是generic则返回false。
函数定义:public bool isHuman;
isInitialized:返回animator是否已成功初始化。
函数定义:public bool isInitialized;
代码示例:
using UnityEngine;

public class AnimationEventExample : MonoBehaviour
{
    Animator animator;
    private void Start()
    {
        animator = GetComponent();
        if (!animator.isInitialized)
            //手动初始化animator
            animator.Rebind();
    }
}
isMatchingTarget:如果自动匹配处于激活状态。
函数定义:public bool isMatchingTarget;
isOptimizable:如果使用AnimatorUtility.OptimizeTransformHierarchy可以优化当前rig,则返回true
函数定义:public bool isOptimizable;
keepAnimatorControllerStateOnDisable:禁用GameObject时控制Animator组件的行为。设置为true以保持Animator controller的当前状态。设置为false以清除Animator控制器的当前状态。默认值为false。此属性是可序列化的,可以保存在Prefab中。此属性适用于与Animator关联的AnimatorController.此属性不会影响AnimatorControllerPlayable.
函数定义:public bool keepAnimatorControllerStateOnDisable;
layerCount:返回controller中的layers数量。
函数定义:public int layerCount;
layersAffectMassCenter:Additional layers会影响质心
函数定义:public bool layersAffectMassCenter;
leftFeetBottomHeight:左脚底部高度。
函数定义:public float leftFeetBottomHeight;
代码示例:
using UnityEngine;

public class AnimationEventExample : MonoBehaviour
{
    Animator animator;
    private void Start()
    {
        animator = GetComponent();
    }
    //模型的Rig选项卡的Animation Type需设置为 Humanoid,Animator的IK Pass需勾选
    private void OnAnimatorIK()
    {
        if(animator)
        {
            Vector3 leftFootT = animator.GetIKPosition(AvatarIKGoal.LeftFoot);
            Quaternion leftFootQ = animator.GetIKRotation(AvatarIKGoal.LeftFoot);
            Vector3 leftFootH = new Vector3(0, -animator.leftFeetBottomHeight, 0);
            Vector3 pos = leftFootT + leftFootQ * leftFootH;
            Debug.Log(pos);
        }
    }
}
parameterCount:返回控制器中parameters的数量
函数定义:
public int parameterCount;
parameters:Animator使用的AnimatorControllerParameterl列表(只读)
函数定义:public AnimatorControllerParameter[] parameters;
pivotPosition:获取pivot的当前位置。pivot是avatar左脚和右脚之间最稳定的点。
函数定义:public Vector3 pivotPosition;
pivotWeight:获取pivot的权重。pivot是avatar左脚跟右脚之间最稳定的点。对于值0,左脚是最稳定的点。对于值1,右脚是最稳定的点。
函数定义:public float pivotWeight;
playableGraph:由animator创建的PlayableGraph
函数定义:public Playables.PlayableGraph playableGraph;
playbackTime:设置录制缓冲区中的播放位置。在播放模式下,此值用于控制缓冲区中的当前播放位置(以秒为单位)。该值可以在recorderStartTime和recorderStopTime之间。
函数定义:public float playbackTime;
recorderMode:获取Animator录制模式
函数定义:public AnimatorRecorderMode recorderMode;
recorderStartTime:相对于调用StartRecording的帧,缓冲区的第一帧的开始时间。例如,如果我们在第10帧开始录制,并且缓冲区是5帧长。如果未初始化缓冲区(未调用StartRecording),则此属性的值将为-1.
函数定义:public float recorderStartTime.
recorderStopTime:记录clip相对于调用StartRecording的结束时间。例如,如果我们在10秒开始录制,并在10秒结束录制,那么它的值为5.如果缓冲区未初始化(未调用StartRecording),则此属性的值将为-1.
函数定义:public float recorderStopTime。
rightFeetBottomHeight:获取右脚底部高度
函数定义:public float rightFeetBottomHeight;
代码示例:
using UnityEngine;

public class AnimationEventExample : MonoBehaviour
{
    Animator animator;
    private void Start()
    {
        animator = GetComponent();
    }
    //模型的Rig选项卡的Animation Type需设置为 Humanoid,Animator的IK Pass需勾选
    private void OnAnimatorIK()
    {
        if(animator)
        {
            Vector3 rightFootT = animator.GetIKPosition(AvatarIKGoal.RightFoot);
            Quaternion rightFootQ = animator.GetIKRotation(AvatarIKGoal.RightFoot);
            Vector3 rightFootH = new Vector3(0, -animator.rightFeetBottomHeight, 0);
            Vector3 pos = rightFootT + rightFootQ * rightFootH;
            Debug.Log(pos);
        }
    }
}
rootPosition:root postion,GameObject的坐标。你只应在OnAnimatorIK()函数调用中设置此值。
函数定义:public Vector3 rootPosition;
rootRatation:root rotation,GameObject的rotation。你只应在OnAnimatorIK()函数调用中设置此值。
函数定义:public Quaternion rootRotation;
runtimeAnimatorController:控制Animator的AnimatorController的运行时表示。在运行时使用基于相同AnimatorController的AnimatorOverrideController交换Animator.runtimeAnimatorController不会重置状态机的当前状态。
函数定义:public RuntimeAnimatorController runtimeAnimatorController
speed:Animator的播放速度。1是正常播放速度。使用Animator.speed来操纵Animator的播放速度。Animator当前正在播放的任何动画都会根据速度的变化而减慢或加速。正常播放时将速度设为1.仅在启用recorder时支持负播放速度。
函数定义:public float speed;
代码示例:
using UnityEngine;

public class AnimationEventExample : MonoBehaviour
{
    Animator m_Animator;
    float m_MySliderValue;
    private void Start()
    {
        m_Animator = gameObject.GetComponent();
    }
    private void OnGUI()
    {
        GUI.Label(new Rect(0, 25, 40, 60), "Speed");
        m_MySliderValue = GUI.HorizontalSlider(new Rect(45, 25, 200, 60), m_MySliderValue, 0.0F, 1.0F);
        m_Animator.speed = m_MySliderValue;
        //Update Mode设置为UnscaledTime受m_Animator.speed不受Time.timeScale影响
        //Time.timeScale = 0;
    }
}
stabilizeFeet:在transition和blend过程中自动稳定feet
函数定义:public bool stabilizeFeet
targetPosition:返回SetTarget指定的目标的位置。该位置仅在SetTarget调用后一帧有效。
函数定义:public Vector3 targetPosition;
targetRotation:返回SetTarget指定的目标的旋转。该旋转仅在SetTarget调用后一帧有效。
函数定义:public Quaternion targetRotation;
updateMode:指定Animator的更新模式
函数定义:public AnimatorUpdateMode updateMode;
velocity:获取上一帧的Avatar速度。
函数定义:public Vector3 velocity;

3.Public Methods

ApplyBuiltinRootMotion:应用默认的root motion。在你不希望手动处理root motion的帧上的MonoBehaviour.OnAnimatorMove或StateMachineBehaviour.OnStateMove中使用此选项。
函数定义:public void ApplyBuiltinRootMotion()
CrossFade:使用normalized时间创建从当前状态到任何其他状态的淡入淡出效果。指定状态名称或用于生成hash的字符串时,它应包含父层的名称。例如:如果Base Layer中有Run state,则名称为Base Layer.Run.
函数定义:
public void CrossFade(string stateName,float normalizedTransitionDuration,int layer = -1,float normalizedTimeOffset = float.NegativeInfinity,float normalizedTransitionTime = 0.0f)
public void CrossFade(int stateHashName,float normalizedTransitionDuration,int layer = -1,float normalizedTimeOffset = 0.0f,float normalizedTransitionTime = 0.0f)
函数参数:
stateName:state的名称
stateHashName:state的名称对应的hash值
normalizedTransitionDuration:transition的持续时间(normalized)
layer:发生淡入淡出效果的层
normalizedTimeOffset:state的时间(normalized)
normalizedTransitionTime:transition的时间(normalize)
CrossFadeInFixedTime:使用以秒为单位创建从当前状态到其他状态的淡入淡出效果。指定状态名称或用于生成hash的字符串时,它应包含父层的名称。例如:如果Base Layer中有Run state,则名称为Base 
函数定义:
public void CrossFadeInFixedTime(int stateHashName,float fixedTransitionDuration,int layer = -1,float fixedTimeOffset = 0.0f,float normalizedTransitionTime = 0.0f)
public void CrossFadeInFixedTime(string stateName,float fixedTransitionDuration,int layer = -1,float fixedTimeOffset = 0.0f,float normalizedTransitionTime = 0.0f)
函数参数:
stateName:state的名称
stateHashName:state的名称对应的hash值
fixedTransitionDuration:transition的持续时间(以秒为单位)
layer:发生淡出淡入效果的层
fixedTimeOffset:state的时间(以秒为单位)
normalizedTransitionTime:transition的时间(normalized)
GetAnimatorTransitionInfo:返回带有当前转换信息的AnimatorTransitionInfo.
函数定义:public AnimatorTransitionInfo GetAnimatorTransitionInfo(int layerIndex);
函数参数:
layerIndex:layer的索引
函数返回值:
AnimatorTransitionInfo:带有当前转换信息的AnimatorTransitionInfo.
GetBehaviour:返回匹配T类型或从T派生的第一个StateMachineBehaviour。如果未找到,则返回null。
函数定义:public T GetBehaviour();
代码示例:
using UnityEngine;

public class SlideBehaviour : StateMachineBehaviour
{
    public Vector3 target;
    public float slideMathTargetStart = 0.11f;
    public float slideMathTargetStop = 0.40f;
    public override void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    {
        animator.MatchTarget(target, new Quaternion(), AvatarTarget.Root, new MatchTargetWeightMask(new Vector3(1, 0, 1), 0), slideMathTargetStart, slideMathTargetStop);
    }
}
using UnityEngine;

public class RunBehaviour : StateMachineBehaviour
{
    public override void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    {
        Transform transform = animator.GetComponent();
        RaycastHit hitInfo;
        Vector3 dir = transform.TransformDirection(Vector3.forward);
        if(Physics.Raycast(transform.position + new Vector3(0,1.5f,0),dir,out hitInfo,10))
        {
            if(hitInfo.collider.tag == "Obstacle")
            {
                animator.GetBehaviour().target = transform.position + 1.25f * hitInfo.distance * dir;
                if (hitInfo.distance < 6)
                    animator.SetTrigger("Slide");
            }
        }
    }
}
GetBehaviours:返回匹配类型T或从T派生的所有StateMachineBehaviour。如果未找到,则返回null。
函数定义:public T[] GetBehaviours();
代码示例:
using UnityEngine;

public class SlideBehaviour : StateMachineBehaviour
{
    public bool fastBreath;
    public override void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    {
        animator.SetBool("FastBreath", fastBreath);
    }
}
using UnityEngine;

public class RunBehaviour : StateMachineBehaviour
{
    public override void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    {
        SlideBehaviour[] slideBehaviours = animator.GetBehaviours();
        for(int i = 0; i < slideBehaviours.Length; i++)
        {
            slideBehaviours[i].fastBreath = true;
        }
    }
}
GetBoneTransform:返回映射到角色骨骼ID的Transform
函数定义:public Transform GetBoneTransform(HumanBodyBones humanBoneId);
函数参数:
humanBoneId:查询的角色骨骼。可以查看HumanBodyBones以获取可能值的列表。
GetBool:返回给定boolean parameter的值。返回Animator Controller中bool parameter的当前状态。使用parameter的名称或ID来搜索对应的值。
函数定义:
public bool GetBool(string name);
public bool GetBool(int ide);
函数参数:
name:parameter的名称
id:parameter的ID
函数返回值:
bool:parameter的值
代码示例:
using UnityEngine;

public class AnimationEventExample : MonoBehaviour
{
    Animator m_Animator;
    bool m_Jump;
    private void Start()
    {
        m_Animator = gameObject.GetComponent();
        m_Jump = false;
    }
    private void Update()
    {
        if(Input.GetKey(KeyCode.Space))
        {
            m_Animator.SetBool("Jump", true);
            if (m_Animator.GetBool("Crouch"))
            {
                m_Animator.SetBool("Crouch", false);
            }
        }
        else
        {
            m_Animator.SetBool("Jump", false);
        }
        if (Input.GetKey(KeyCode.DownArrow))
            m_Animator.SetBool("Crouch", true);
        else
            m_Animator.SetBool("Crouch", false);
    }
}
GetCurrentAnimatorClipInfo:返回给定Layer当前状态下所有AnimatorClipInfo的数组
函数定义:
public AnimatorClipInfo[] GetCurrentAnimatorClipInfo(int layerIndex)
public void GetCurrentAnimatorClipInfo(int layerIndex,Listclips):使用给定Layer当前状态中的所有AnimatorClipInfo的列表填充clip
函数参数:
layerIndex:layer索引
clip:要填充的AnimatorClipInfo列表
函数返回值:
AnimatorClipInfo[] 当前状态下的AnimatorClipInfo数组。
代码示例:
using UnityEngine;

public class AnimationEventExample : MonoBehaviour
{
    Animator m_Animator;
    string m_ClipName;
    AnimatorClipInfo[] m_CurrentClipInfo;
    float m_CurrentClipLength;
    private void Start()
    {
        m_Animator = gameObject.GetComponent();
        m_CurrentClipInfo = this.m_Animator.GetCurrentAnimatorClipInfo(0);
        m_CurrentClipLength = m_CurrentClipInfo[0].clip.length;
        m_ClipName = m_CurrentClipInfo[0].clip.name;
    }
    private void OnGUI()
    {
        GUI.Label(new Rect(0, 0, 200, 20), "Clip Name : " + m_ClipName);
        GUI.Label(new Rect(0, 30, 200, 20), "Clip Length : " + m_CurrentClipLength);
    }
}
GetCurrentAnimatorClipInfoCount:返回当前状态下AnimatorClipInfo的数量。
函数定义:public int GetCurrentAnimatorClipInfoCount(int layerIndex)
函数参数:
layerIndex:layer的索引
函数返回值:
int:当前状态下的AnimatorClipInfo的数量
GetCurrentAnimatorStateInfo:返回带有当前状态信息的AnimatorStateInfo
函数定义:public AnimatorStateInfo GetCurrentAnimatorStateInfo(int layerIndex);
函数参数:
layerIndex:Layer的索引
函数返回值:
AnimatorStateInfo:包含当前状态信息的AnimatorStateInfo。
代码示例:从Animator中的当前状态获取数据。使用此命令从状态获取详细信息,包括访问状态的速度,长度,名称和其他变量。
using UnityEngine;

public class AnimationEventExample : MonoBehaviour
{
    Animator m_Animator;
    float m_CurrentSpeed;
    private void Start()
    {
        m_Animator = gameObject.GetComponent();
        m_CurrentSpeed = m_Animator.GetCurrentAnimatorStateInfo(0).speed;
    }
    private void Update()
    {
        if(Input.GetKeyDown(KeyCode.Space))
        {
            m_Animator.SetTrigger("Jump");
        }
        if(m_Animator.GetCurrentAnimatorStateInfo(0).IsName("Jump"))
        {
            Debug.Log("Jumping");
        }
    }
    private void OnGUI()
    {
        GUI.Label(new Rect(25, 25, 200, 20), "Speed of State : " + m_CurrentSpeed);
    }
}
GetFloat:返回给定float parameter参数的值.。如果指定的float参数不存在,则float 返回0
函数定义:
public float GetFloat(string name);
public float GetFloat(int id);
函数参数:
name:parameter的名称
id:parameter的ID
函数返回值:
float:parameter的值
GetIKHintPosition:获取IK hint的位置
函数定义:public Vector3 GetIKHintPosition(AvatarIKHint hint)
函数参数:
hint:被查询的AvatarIKHint
函数返回值:
Vector3:返回此IK hint在世界空间中的当前位置。
GetIKHintPositionWeight:获取IK hint的转换权重(0=在IK之前的原始动画处,1=在hint处)
函数定义:public float GetIKHintPositionWeight(AvatarIKHint hint);
函数参数:
hint:被查询的AvatarIKHint
函数返回值:
float:返回转换的权重
GetIKPosition:获取IK goal的位置。IK goal是特定身体部位的目标位置和旋转。Unity可以计算如何从起点向目标移动身体部位(即从动画中获得当前位置和旋转)。此函数获取世界空间中指定目标的当前位置。
函数定义:public Vector3 GetIKPosition(AvatarIKGoal goal);
函数参数:
goal:被查询的AvatarIKGoal
函数返回值:
Vector3:返回IK goal在世界空间中的当前位置。
GetIKPositionWeight:获取IK goal的转换权重(0=在IK之前的原始动画处,1=在goal处)。IK goal是特定身体部位的目标位置和旋转。Unity可以计算如何从起点向目标移动身体部位(即从动画中获得当前位置和旋转)。IK计算的点也受0-1范围内的权重值的影响,该权重值决定了目标开始和目标之间的距离。此函数返回目标位置的当前权重值。
函数定义:public float GetIKPositionWeight(AvatarIKGoal goal);
函数参数:
goal:被查询的AvatarIKGoal
GetIKRotation:获取IK goal的旋转。IK goal是特定身体部位的目标位置和旋转。Unity可以计算如何从起点向目标移动身体部位(即从动画中获得当前位置和旋转)。此函数获取世界空间中指定目标的当前位置。
函数定义:public Quaternion GetIKRotation(AvatarIKGoal goal);
函数参数:
goal:被查询的AvatarIKGoal
GetIKRotationWeight:获取IK goal的旋转权重(0=IK之前的旋转,1=IK goal的旋转)。IK goal是特定身体部位的目标位置和旋转。Unity可以计算如何从起点向目标移动身体部位(即从动画中获得当前位置和旋转)。IK计算的点也受0-1范围内的权重值的影响,该权重值决定了目标开始和目标之间的距离。此函数返回目标位置的当前权重值。
函数定义:public float GetIKRotationWeight(AvatarIKGoal goal);
函数参数:
goal:被查询的AvatarIKGoal
GetInteger:返回给定int parameter的值
函数定义:
public int GetInteger(string name);
public int GetInteger(int id);
函数参数:
name:parameter的名称
id:parameter的ID
函数返回值:
int:parameter的值
GetLayerIndex:返回具有给定名称的Layer的索引
函数定义:public int GetLayerIndex(string layerName);
函数参数:
layerName:layer名称
函数返回值:
int:layer的索引
GetLayerName:返回layer的名称
函数定义:public string GetLayerName(int layerIndex);
函数参数:
layerIndex:layer的索引
函数返回值:
string:layer的名称
GetLayerWeight:返回给定索引的Layer的权重。
函数定义:public float GetLayerWeight(int layerIndex);
函数参数:
layerIndex:Layer的索引
函数返回值:
float:Layer的权重
GetNextAnimatorClipInfo:返回给定Layer的下一个状态中的所有AnimatorClipInfo的数组。
public void GetNextAnimatorClipInfo(int layerIndex,Listclips);
函数定义:
public AnimatorClipInfo[] GetNextAnimatorClipInfo(int layerIndex);
public void GetNextAnimatorClipInfo(int layerIndex,Listclips):在给定Layer的下一个状态中使用AnimatorClip
函数参数:
layerIndex:layer的索引
clips:要填充的AnimatorClipInfo列表
函数返回值:
AnimatorClipInfo[]:下一个状态的所有AnimatorClipInfo的数组
GetNextAnimatorClipInfoCount:返回下一个状态中AnimatorClipInfo的数量。
函数定义:public int GetNextAnimatorClipInfoCount(int layerIndex);
函数参数:
layerIndex:Layer的索引
函数返回值:
int:下一个状态的AnimatorClipInfo的数量
GetNextAnimatorStateInfo:返回带有下一个状态信息的AnimatorStateInfo。
函数定义:public AnimatorStateInfo GetNextAnimatorStateInfo(int layerIndex)
函数参数:
layerIndex:Layer的索引
函数返回值:
AnimatorStateInfo:下一个状态信息的AnimatorStateInfo.
GetParameter:参阅AnimatorController.parameters
函数定义:public AnimatorControllerParameter GetParameter(int index);
HasState:如果此Layer中存在状态,则返回true,否则返回false。
函数定义:public bool HasState(int layerIndex,int stateID);
函数参数:
layerIndex:Layer的索引
stateID:state ID
函数返回值:
bool:如果此Layer中存在状态,则为true,否则为false。
InterruptMatchTarget:中断自动目标匹配。Completely将使游戏对象在下一帧完全匹配目标
函数定义:
public void InterruptMatchTarget();
public void InterruptMatchTarget(bool completeMatch = true);
IsInTransition:如果给定Layer存在transition,则返回true,否则返回false;
函数定义:public bool IsInTransition(int layerIndex);
函数参数:
layerIndex:Layer的索引
函数返回值:
bool:如果给定Layer上存在transition则为true,否则返回false。
IsParameterControlledByCurve:如果参数由曲线控制,则返回true,否则返回false。(可以用Animation控制Animator的参数)
函数参数:
name:parameter的名称
id:parameter的ID
函数返回值:
bool:如果参数由曲线控制,则返回true,否则返回false。
MatchTarget:自动调整GameObject的position和rotation。调整GameObject的位置和旋转,以便当前状态处于指定进度时AvatarTarget到达matchPosition。target match仅适用于base Layer(索引0)。你一次只能排队一个match target,并且必须等待第一个match target完成,否则你的match target将被丢弃。如果你使用低于clip当前normalize时间的开始时间调用Match Target并且clip可以循环,则Match Target将调整时间以匹配下一个cip循环。例如:开始时间=0.2,当前normalize时间 = 0.3,则开始时间调整为1.2。
函数定义:
public void MatchTarget(Vector3 matchPosition,Quaternion matchRotation,AvatarTarget targetBodyPart,MatchTargetWeightMask weightMask,float startNormalizedTime,float targetNormalizedTime = 1);
函数参数:
matchPosition:我们希望身体部位到达的位置
matchRotation:我们想要身体部位的旋转
targetBodyPart:参与匹配的身体部位
weightMask:包含匹配位置和旋转的权重的结构体
startNormalizedTime:animation clip中的开始时间(0-片段的开头,1-片段的结尾)
targetNormalizedTime:animation clip中的结束时间(0-片段的开头,1-片段的结尾),大于1的值可以设置为在一定数量的循环后触发匹配。例:2.3表示第二次循环的30%处。
代码示例:
using UnityEngine;

public class AnimationEventExample : MonoBehaviour
{
   public void MatchTarget(Vector3 matchPosition,Quaternion matchRotation,AvatarTarget target,
       MatchTargetWeightMask weightMask,float normalisedStartTime,float normalisedEndTime)
    {
        var animator = GetComponent();
        if (animator.isMatchingTarget)
            return;
        float normalizeTime = Mathf.Repeat(animator.GetCurrentAnimatorStateInfo(0).normalizedTime, 1f);
        if (normalizeTime > normalisedEndTime)
            return;
        animator.MatchTarget(matchPosition, matchRotation, target, weightMask, normalisedStartTime, normalisedEndTime);
    }
}
Play:播放一个状态。指定state名称或用于生成hash的字符串时,它应包含父Layer的名称。例如,如果Base Layer有Run State,则名称为Base Layer.Run。normalizedTime参数在0-1之间变化。如果此参数保持为0,则Play将按预期运行。可以给出不同的起点
函数定义:
public void Play(string stateName,int layer = -1,float normalizedTime = float.NegativeInfinity);
public void Play(int stateNameHash,int layer = -1,float normalizedTime = float.NegativeInfinity);
函数参数:
stateName:state的名称
stateNameHash:state名称对应的hash。如果stateNameHash为0,则更改当前state的时间。
layer:Layer索引。如果Layer为-1,则它将播放具有给定state名称或hash的第一个状态。
normalizedTime:0-1之间的时间偏移。如果此参数保持为0,则Play将按预期运行。可以给出不同的起点。一个例子可以将normalizedTime设置为0.5,这意味着动画从中途开始。如果从一个状态转换到另一个状态,则可以混合也可以不混合。如果转换从0.75开始,他将与另一个状态混合。如果未设置transition,则Play将继续为1.0而不进行任何更改。
代码示例:这个立方体有两个Animator状态叫做Rest和Bounce。在Rest状态下播放空动画。按下Space键后,立方体将切换到Bounce状态。这会导致立方体上下跳动两次。然后返回Rest状态。因为从Animator.Play脚本中选择了Bound,所以不需要transition,但是从Bounce到Rest的返回需要一个Transition。勾选Has Exit Time使Bounce持续一秒钟。将此脚本附加到要设置动画的GameObject。
using UnityEngine;

public class AnimationEventExample : MonoBehaviour
{
    private Animator anim;
    private void Start()
    {
        anim = GetComponent();
    }
    private void Update()
    {
        if(Input.GetKeyDown(KeyCode.Space))
        {
            if(null != anim)
            {
                anim.Play("Bounce", 0, 0.25f);
            }
        }
    }
}
PlayInFixedTime:播放一个状态。指定state名称或用于生成hash的字符串时,它应包含父Layer的名称。例如,如果Base Layer有Run State,则名称为Base Layer.Run。
函数定义:
public void PlayInFixedTime(string stateName,int layer = -1,float fixedTime = float.NegativeInfinity);
public void PlayInFixedTime(int stateNameHash,int layer = -1,float fixedTime = float.NegativeInfinity);
函数参数:
stateName:state的名称
stateNameHash:state名称对应的hash。如果stateNameHash为0,则更改当前state的时间。
layer:Layer索引。如果Layer为-1,则它将播放具有给定state名称或hash的第一个状态。
fixedTime:时间偏移(以秒为单位)
Rebind:使用Animator重新绑定所有动画属性和网格数据。当你通过脚本手动更改GameObject的hierarchy时,可以使用此函数,例如组合mesh或交互完整的transform hierarchy。
函数定义:public void Rebind();
ResetTrigger:重置给定Trigger的值。使用此选项可在Animator Controller中重置仍可激活的Trigger参数。确保在Animator Controller中使用相同的名称创建参数。
函数定义:
public void ResetTrigger(string name);
public void ResetTrigger(int id);
函数参数:
name:parameter的名称
id:parameter的ID
代码示例:
using UnityEngine;

public class AnimationEventExample : MonoBehaviour
{
    Animator m_Animator;
    private void Start()
    {
        m_Animator = gameObject.GetComponent();
    }
    private void Update()
    {
        if (Input.GetKey(KeyCode.UpArrow))
        {
            m_Animator.ResetTrigger("Crouch");
            m_Animator.SetTrigger("Jump");
        }
        if(Input.GetKey(KeyCode.DownArrow))
        {
            m_Animator.ResetTrigger("Jump");
            m_Animator.SetTrigger("Crouch");
        }
    }
}
SetBoneLocalRotation:在IK pass期间设置人体骨骼的局部旋转。可用于为任何人体骨骼创建IK goals的旋转。Ex:通过在IK pass 期间设置臀部和脊椎局部旋转来独立控制下半身和上半身。
函数定义:
public void SetBoneLocalRotation(HumanBodyBones humanBoneId,Quaternion rotation);
函数参数:
humanBoneId:人体骨骼ID
rotation:局部rotation
SetBool:设置给定bool参数的值。使用Animator.SetBool通过脚本将布尔值传递给Animator Controller。用它用触发Animator state之间的装换。例如,通过将"alive"布尔值设置为false来触发死亡动画。注意:你可以按名称或ID识别parameter,但名称或称号必须与要在Animator中更改的参数相同。
函数定义:
public void SetBool(string name,bool value);
public void SetBool(int id,bool value);
函数参数:
name:parameter的名称
id:parameter的ID
value:parameter的新的值
代码示例:
using UnityEngine;

public class AnimationEventExample : MonoBehaviour
{
    Animator m_Animator;
    bool m_Jump;
    private void Start()
    {
        m_Animator = gameObject.GetComponent();
        m_Jump = false;
    }
    private void Update()
    {
        if (Input.GetMouseButton(0))
            m_Jump = true;
        else
            m_Jump = false;
        if (m_Jump == false)
            m_Animator.SetBool("Jump", false);
        if (m_Jump == true)
            m_Animator.SetBool("Jump", true);
    }
}
SetFloat:将float值发送到Animator以影响transitions。
函数定义:
public void SetFloat(string name,float value);
public void SetFloat(string name,float value,float dampTime,float deltaTime);
public void SetFloat(int id,float value);
public void SetFloat(int id,float value,float dampTime,float deltaTime);
函数参数:
name:parameter的名称
id:parameter的ID
value:parameter的新值
dampTime:damper的总时间
deltaTime:给damper的增量时间
代码示例:在脚本中使用SetFloat将浮点值发送到Animator以激活transitions。在Animator中,定义哪些值会影响某些动画的过渡方式。在这各种情况下都很有用,游戏是在动画循环中,例如移动动画,你可能需要根据所施加的按钮压力来行走或跑步。
using UnityEngine;

public class AnimationEventExample : MonoBehaviour
{
    Animator m_Animator;
    float m_HorizontalMovement;
    private void Start()
    {
        m_Animator = gameObject.GetComponent();
    }
    private void Update()
    {
        m_HorizontalMovement = Input.GetAxis("Horizontal");
        m_Animator.SetFloat("horizontalSpeed", m_HorizontalMovement);
    }
}

[外链图片转存失败(img-rMg6ar08-1562143080772)(https://docs.unity3d.com/StaticFiles/ScriptRefImages/AnimatorSetFloat.png)]

SetIKHintPosition:设置IK Hint的位置
函数定义:public void SetIKHintPosition(AvatarIKHint hint,Vector3 hintPosition)
函数参数:
hint:设置的AvatarIKHint
hintPosition:世界空间下的位置
SetIKHintPositionWeight:设置IK Hint的translative权重(0=在IK之前的原始动画,1=在hint处)
函数定义:public void SetIKHintPositionWeight(AvatarIKHint hint,float value);
函数参数:
hint:设置的AvatarIKHint
value:translative的权重
SetIKPosition:设置IK goal的位置。IK goal是特定身体部位的目标位置和旋转。Unity可以计算如何从起点向目标移动身体部位(即从动画中获得的当前位置和旋转)。该功能设定了世界空间的最终目标的位置,身体部位最终的空间中的实际点也受到权重参数的影响,该权重参数指定IK应该瞄准的起点和目标点之间的距离(0-1范围内的值)
函数定义:public void SetIKPosition(AvatarIKGoal goal,Vector3 goalPosition);
函数参数:
goal:设置的AvatarIKGoal
goalPostion:世界空间下的位置
代码示例:
using UnityEngine;

public class AnimationEventExample : MonoBehaviour
{
    Transform objToPickUp;
    Animator animator;
    private void Start()
    {
        animator = GetComponent();
    }
    private void OnAnimatorIK(int layerIndex)
    {
        float reach = animator.GetFloat("RightHandReach");
        animator.SetIKPositionWeight(AvatarIKGoal.RightHand, reach);
        animator.SetIKPosition(AvatarIKGoal.RightHand, objToPickUp.position);
    }
}
SetIKPositionWeight:设置IK goal的translative权重(0=在IK之前的原始动画,1=在goal处)。IK goal是特定身体部位的目标位置和旋转。Unity可以计算如何从起点向目标移动身体部位(即从动画中获得的当前位置和旋转)。此功能设置0-1范围内的权重值,以确定IK将瞄准的起始位置和目标位置之间的距离。位置本身使用SetIKPosition单独设置。
函数定义:
public void SetIKPositionWeight(AvatarIKGoal goal,float value);
函数参数:
goal:设置的AvatarIKGoal;
value:translative的权重
SetIKRotation:设置IK goal的旋转。IK goal是特定身体部位的目标位置和旋转。Unity可以计算如何从起点向目标移动身体部位(即从动画中获得的当前位置和旋转)。该功能设定了世界空间的最终目标的旋转,身体部位最终的实际旋转也受到权重参数的影响,该权重参数指定IK应该瞄准的起点和目标点之间的距离(0-1范围内的值)
函数定义:public void SetIKRotation(AvatarIKGoal goal,Quaternion goalRotation);
函数参数:
goal:设置的AvatarIKGoal
goalRotation:世界空间的rotation
代码示例:
using UnityEngine;

public class AnimationEventExample : MonoBehaviour
{
    Transform objToAimAt;
    Animator animator;
    private void Start()
    {
        animator = GetComponent();
    }
    private void OnAnimatorIK(int layerIndex)
    {
        Quaternion handRotation = Quaternion.LookRotation(objToAimAt.position - transform.position);
        animator.SetIKRotationWeight(AvatarIKGoal.RightHand, 1.0f);
        animator.SetIKRotation(AvatarIKGoal.RightHand, handRotation);
    }
}
SetIKRotationWeight:设置IK goal的旋转权重(0=IK 之前的旋转,1=IK goal的旋转)。IK goal是特定身体部位的目标位置和旋转。Unity可以计算如何从起点向目标移动身体部位(即从动画中获得的当前位置和旋转)。此功能设置0-1范围内的权重值,以确定IK将瞄准的开始和目标旋转之间的距离。目标本身是使用SetIKRotation单独设置的。
函数定义:public void SetIKRotationWeight(AvatarIKGoal goal,float value);
函数参数:
goal:设置的AvatarIKGoal
value:rotational的权重
SetInteger:设置给定整数参数的值。使用它作为触发Animator state之间转换的方法。使用Integers而不是Floats或Booleans的一种方法,是将它用于具有多个状态的东西,例如:方向(向左转,向右转等)。每个方向可以对应一个数字,而不是每次都必须重置多个布尔值。注意:你可以按名称或ID号识别参数,但名称或ID号必须与要在Animator中更改的参数相同。
函数定义:
public void SetInteger(string name,int value);
public void SetInteger(int id,int value);
函数参数:
name:parameter的名称
id:parameter的ID
value:parameter的新值
代码示例:
using UnityEngine;

public class AnimationEventExample : MonoBehaviour
{
    Animator m_Animator;
    private void Start()
    {
        m_Animator = GetComponent();
    }
    private void Update()
    {
        if (Input.GetAxis("Horizontal") > 0 || Input.GetAxis("Horizontal") < 0)
            m_Animator.SetInteger("States", 1);
        else if (Input.GetKey(KeyCode.DownArrow))
            m_Animator.SetInteger("States", 2);
        else if (Input.GetKey(KeyCode.Space))
            m_Animator.SetInteger("States", 3);
        else
            m_Animator.SetInteger("States", 0);
    }
}
SetLayerWeight:设置给定索引处Layer的权重。
函数定义:public void SetLayerWeight(int layerIndex,float weight);
函数参数:
layerIndex:Layer的索引
weight:新的Layer权重
SetLookAtPosition:设置面向的位置
函数定义:public void SetLookAtPosition(Vector3 lookAtPosition)
函数参数:
lookAtPosition:面向的位置
SetLookAtWeight:设置面向的权重
函数定义:
public void SetLookAtWeight(float weight);
public void SetLookAtWeight(float weight,float bodyWeight);
public void SetLookAtWeight(float weight,float bodyWeight,float headWeight);
public void SetLookAtWeight(float weight,float bodyWeight,float headWeight,float eyesWeight);
public void SetLookAtWeight(float weight,float bodyWeight = 0.0f,float headWeight = 1.0f,float eyesWeight = 0.0f,float clampWeight = 0.5f);
函数参数:
weight:(0-1)LookAt的全局权重,其他参数的乘数
bodyWeight:(0-1)确定LookAt身体的权重
headWeight:(0-1)确定LookAt头部的权重
eyesWeight:(0-1)确定LookAt眼睛的权重
clampWeight:(0-1)0表示角色在运动中完全不受约束,1.0表示他完全被约束(无法look at),0.5表示他能够移动一般可能的范围(180度)。
SetTarget:为当前状态设置AvatarTarget和targetNormalizedTime.下一帧后使用Animator.targetPosition和Animator.targetRotation查询位置和旋转。
函数定义:
public void SetTarget(AvatarTarget targetIndex,float targetNormalizedTime);
函数参数:
targetIndex:被查询的avatar body part
targetNormalizedTime:当前状态查询的时间。
SetTrigger:设置给定trigger参数的值。此方法允许你设置(即激活)animation trigger,以使animator controller的状态机中的流量发生变化。Animation Parameters页面描述了Animator Controller Parameters窗口的用途。Trigger是4种可选选项之一。选择此选项会将Trigger添加到所选参数列表中。一旦将其添加到选定列表,就可以对其进行命名。与具有相同true/false选项的bool不同,Triggers具有自动返回false的true选项。一个典型的例子可能是有一个Jump选项。如果在运行期间输入此选项,则角色跳转。在跳转结束时,将返回上一个动作(可能是步行或跑步状态)。
函数定义:
public void SetTrigger(string name);
public void SetTrigger(int id);
函数参数:
name:parameter的名称
id:parameter的ID
StartPlayback:设置animator进入播放模式。在播放模式下,你可以通过设置时间值来控制animator。animator不会从游戏逻辑更新。而是使用playbackTime显示操作时间进度。
函数定义:public void StartPlayback();
代码示例:
using UnityEngine;

public class AnimationEventExample : MonoBehaviour
{
    public Animator m_Animator;
    bool mIsStartPlayback;
    float mTime;
   
    private void Update()
    {
        if (mIsStartPlayback)
        {
            mTime += Time.deltaTime;
            if(m_Animator.recorderStopTime > mTime)
            {
                m_Animator.playbackTime = mTime;
                Debug.Log("animator.playbackTime: " + m_Animator.playbackTime);
            }
        }
        else
        {
            Debug.Log("animator.recorderStartTime: " + m_Animator.recorderStartTime);
            Debug.Log("animator.recorderStopTime: " + m_Animator.recorderStopTime);
        }
    }
    //右键Inspector上的AnimationEventExample脚本点击StartRecording
    [ContextMenu("StartRecording")]
    void StartRecording()
    {
        m_Animator.StartRecording(0);
    }
    [ContextMenu("StopRecording")]
    void StopRecording()
    {
        m_Animator.StopRecording();
    }
    [ContextMenu("StartPlayback")]
    void StartPlayback()
    {
        //必须先调用m_Animator.StartRecording(),然后调用m_Animator.StopRecording(),才能调用此函数
        m_Animator.StartPlayback();
        mTime = m_Animator.recorderStartTime;
        mIsStartPlayback = true;
    }
    [ContextMenu("StopPlayback")]
    void StopPlayback()
    {
        m_Animator.StopPlayback();
        mIsStartPlayback = false;
    }
}
StartRecording:设置animator为recording模式,并分配大小为frameCount的循环缓冲区。调用这个函数之后,recorder开始收集缓冲区中的frameCount帧。注意,在调用StopRecording之前start playback。
函数定义:public void StartRecording(int frameCount);
函数参数:
frameCount:将记录的帧数(更新)。如果frameCount为0,则录制将继续,直到用户调用StopRecording。frameCount的最大值为10000。
StopPlayback:停止animator的playback模式。当播放停止时,avatar恢复从游戏逻辑控制。
函数定义:public void StopPlayback();
StopRecording:停止animator record模式。对StopRecording的调用会将记录缓冲区的内容锁定在当前状态。保存数据以便使用StartPlayback进行后续播放。
函数定义:public void StopRecording();
update:基于deltaTime评估(Evaluates)animator.使用此函数更新animator,可能不适用于物理引擎或Game循环正常evaluated的任何其他系统。
函数定义:public void Update(float deltaTime);
函数参数:
deltaTime:时间增量
WriteDefaultValues:强制写入存储在Animator中的默认值
函数定义:public void WriteDefaultValues();

4、Static Methods

StringToHash:从字符串生成parameter id。这个可用于优化get和set parameter。
函数定义:public static int StringToHash(string name);
函数参数:
name:要转换为Id的字符串

5、Inherited Members
(1).Properties

Behaviour.enabled:为true则启用update,否则禁止
Behaviour.isActiveAndEnabled:脚本是否已激活且已启用。
Component.gameObject:此组件附加到的游戏对象。组件始终附加到游戏对象上。
Component.tag:这个GameObject的tag。
Component.transform:附加到此GameObject的Transform。
Object.hideFlags:隐藏对象,使用场景保存还是用户可修改。
Object.name:对象的名称。

(2).Public Methods

Component.BroadcastMessage:在此游戏对象或其任何子对象的每个MonoBehaviour上调用名为methodName的方法。
Component.CompareTag:这个游戏对象是此标签标记的吗?
Component.GetComponent:如果游戏对象附加了一个此类型的组件,则返回Type类型的组件,如果没有,则返回null。
Component.GetComponentInChildren:使用深度优先搜索返回GameObject或其任何子节点中Type类型的组件。仅当在激活的GameObject上找到组件时才返回该组件。
Component.GetComponentInParent:返回GameObject或其任何父项中的Type类型的组件。
Component.GetComponents:返回GameObject中Type类型的所有组件。
Component.GetComponentsInChildren:返回GameObject或其任何子项中Type类型的所有组件。
Component.GetComponentsInParent:返回GameObject或其任何父项中Type类型的所有组件。
Component.SendMessage:在此游戏对象中的每个MonoBehaviour上调用名为methodName的方法。
Component.SendMessageUpwards:在此游戏对象中的每个MonoBehaviour以及脚本的每个父节点上调用名为methodName的方法。
Object.GetInstanceID:返回对象的实例ID。始终保证对象的实例ID是唯一的。
Object.ToString:返回GameObject的名称

(3).Static Methods

Object.Destroy:删除GameObject、Component或者asset。
Object.DestroyImmediate:立即销毁对象obj。强烈建议你使用Destroy。
Object.DontDestroyOnLoad:加载新场景时,请勿销毁目标对象。
Object.FindObjectOfType:返回Type类型的第一个激活的加载对象。
Object.FindObjectsOfType:返回Type类型的所有激活的已加载对象的列表。
Object.Instantiate:克隆原始对象,并返回克隆的对象。

(4).Operators

Object.bool:对象是否存在
Object.operator !=:比较两个对象是否引用不同的对象
Object.operator ==:比较两个对象引用以查看它们是否引用同一对象

十五、AnimatorCullingMode
1.描述
  Animator的cull mode
2.Properties

AlwaysAnimate:始终为整个角色播放动画。即使在屏幕外,对象也播放动画。
函数定义:AnimatorCullingMode.AlwaysAnimate
CullUpdateTransforms:当渲染器不可见时,将禁用重定向(Retarget),IK,transform写入。始终会评估状态机和root motion.因此,你将始终受到OnAnimatorMove回调。如果角色不可见,则将跳过所有其他动画,具体评估骨骼动画,IK,OnAnimatorIK将被跳过。注意:动画仍将在Scene视图中可见,即它不受动画剔除的影响。
函数定义:AnimatorCullingMode.CullUpdateTransforms
CullCompletely:当渲染器不可见时,动画将完全禁用。注意:动画仍将在Scene视图中可见,即它不受动画剔除的影响。
函数定义:AnimatorCullingMode.CullCompletely

十六、AnimatorControllerParameter
1.描述
  用于在脚本和控制器之间进行通信。某些参数可以在脚本中设置并由控制器使用,而其他参数基于Animation Clips中的Custom Curves,可以使用脚本API进行采样。
2.Properties

defaultBool:参数的默认bool值
函数定义:public bool defaultBool;
defaultFloat:参数的默认float值
函数定义:public float defaultFloat;
defaultInt:参数的默认int值
函数定义:public int defaultInt;
name:参数的名称
函数定义:public string name
nameHash:返回参数的名字的hash
函数定义:public int nameHash;
type:参数类型
函数定义:public AnimatorControllerParameterType type;

十七、AnimatorControllerParameterType
1.描述
  参数类型,可以是bool,float,int或者trigger
2.Properties

Float:Float类型参数
函数定义:AnimatorControllerParameterType.Float
Int:Int类型参数
函数定义:AnimatorControllerParameterType.Int
Bool:Bool类型参数
函数定义:AnimatorControllerParameterType.Bool
Trigger:Trigger类型参数.Trigger类似于bool参数,但在Transition中使用时,它们的值会重置为false.
函数定义:AnimatorControllerParameterType.Trigger

十八、AnimatorRecorderMode
1.描述
  录制可以是Offline,Playback,Record三种模式
2.Properties

Offline:Animator录制模式为Offline
函数定义:AnimatorRecorderMode.Offline
Playback:Animator录制模式为Playback
函数定义:AnimatorRecorderMode.Playback
Record:Animator录制模式为Record
函数定义 :AnimatorRecorderMode.Record

十九、RuntimeAnimatorController
1.描述
  AnimatorController的运行时表示。使用此表示在运行时更改Animator Controller
2.Properties

animationClips:检索控制器使用的所有AnimationClip
函数定义:public AnimationClip[] animationClips;

3.Inherited Members
(1).Properties

Object.hideFlags:隐藏对象,使用场景保存还是用户可修改。
Object.name:对象的名称。组件与游戏对象和所有附加组件共享相同的名称。如果一个类派生自MonoBehaviour,它将从MonoBehaviour继承"name"字段。如果此类也附加到GameObject,则"name"字段将设置为GameObject的名称。

(2).Public Methods

Object.GetInstanceID:返回对象的实例ID。始终保证对象的实例ID是唯一的。
Object.ToString:返回GameObject的名称

(3).Static Methods

Object.Destroy:删除GameObject、Component或者asset。
Object.DestroyImmediate:立即销毁对象obj。强烈建议你使用Destroy。
Object.DontDestroyOnLoad:加载新场景时,请勿销毁目标对象。
Object.FindObjectOfType:返回Type类型的第一个激活的加载对象。
Object.FindObjectsOfType:返回Type类型的所有激活的已加载对象的列表。
Object.Instantiate:克隆原始对象,并返回克隆的对象。

(4).Operators

Object.bool:对象是否存在
Object.operator !=:比较两个对象是否引用不同的对象
Object.operator ==:比较两个对象引用以查看它们是否引用同一对象

二十、AnimatorUpdateMode
1.描述
  Animator的更新模式
2.Properties

Normal:Animator正常更新
函数定义:AnimatorUpdateMode.Normal;
AnimatePhysics:在物理循环期间更新Animator,以使动画系统与物理引擎系统同步。
函数定义:AnimatorUpdateMode.AnimatePhysics.
UnscaledTime:Animator独立于Time.timeScale更新。这通常用于在游戏暂停时,仍想播放UI动画的情况。
函数定义:AnimatorUpdateMode.UnscaledTime.

二十一、AnimatorTransitionInfo
1.描述
  当前transition的信息
2.Properties

anyState:如果transition来自AnyState节点或Animator.CrossFade,则返回true.
函数定义:public bool anyState.
duration:transition的持续时间。根据AnimatorTransitionInfo.durationUnit,持续时间可以用秒表示(即DurationUnit.Fixed)或以百分比表示(即DurationUnity.Normalized)。normalized持续时间基于source state持续时间。注意:以秒为单位转换的normalized时间可以在帧之间变化,因为source state持续时间可以根据变化因素(如blendtree中的权重)而变化。
函数定义:public float duration;
durationUnit:transition持续时间的单位。可以是DurationUnit.Fixed(以秒为单位)或DurationUnit.Normalized(以百分比表示)
函数定义:public DurationUnit durationUnit;
fullPathHash:Transition的哈希名称。格式为“FULLPATH.CURRENT_STATE->FULLPATH.NEXT_STATE”。使用Animator.StringToHash生成哈希。
函数定义:public int fullPathHash
nameHash:Transition的简化名称。格式为"CURRENT_STATE->NEXT_STATE"。使用Animator.StringToHash生成哈希。
函数定义:public int nameHash
normalizedTime:transition的normalized时间.0.0f到1.0f
函数定义:public float normalizedTime;
userNameHash:用户指定的Transition名称

3.Public Methods

IsName:名称是否与激活的Transition的名称匹配。格式为"CURRENT_STATE->NEXT_STATE".
函数定义:public bool IsName(string name);
IsUserName:userName是否与活动Transition的名称匹配。
函数定义:public bool IsUserName(string name);

二十二、DurationUnit
1.描述
  描述持续时间的单位
2.Properties

Fixed:固定(fixed)持续时间是以秒表示的持续时间。
函数定义:DurationUnit.Fixed
Normalized:normalized持续时间是以百分比表示的持续时间。
函数定义:DurationUnit.Normalized

二十三、HumanBodyBones
1.描述
  人体骨骼
2.Properties

Hips:这是臀部骨骼
函数定义:HumanBodyBones.Hips
LeftUpperLeg:这是左上腿骨
函数定义:HumanBodyBones.LeftUpperLeg;
RightUpperLeg:这是右上腿骨
函数定义:HumanBodyBones.RightUpperLeg;
LeftLowerLeg:这是左膝骨
函数定义:HumanBodyBones.LeftLowerLeg;
RightLowerLeg:这是右膝骨
函数定义:HumanBodyBones.RightLowerLeg;
LeftFoot:这是左脚踝骨
函数定义:HumanBodyBones.LeftFoot;
RightFoot:这是右脚踝骨
函数定义:HumanBodyBones.RightFoot;
Spine:这是第一个脊骨
函数定义:HumanBodyBones.Spine
Chest:这是胸骨
函数定义:HumanBodyBones.Chest
UpperChest:这是上胸部骨骼
函数定义:HumanBodyBones.UpperChest
Neck:这是颈骨
函数定义:HumanBodyBones.Neck
Head:这是头骨
函数定义:HumanBodyBones.Head
LeftShoulder:这是左肩骨
函数定义:HumanBodyBones.LeftShoulder
RightShoulder:这是右肩骨
函数定义:HumanBodyBones.RightShoulder
LeftUpperArm:这是左上臂骨
函数定义:HumanBodyBones.LeftUpperArm
RightUpperArm:这是右上臂骨
函数定义:HumanBodyBones.RightUpperArm
LeftLowerArm:这是左肘骨
函数定义:HumanBodyBones.LeftLowerArm
RightLowerArm:这是右肘骨
函数定义:HumanBodyBones.RightLowerArm
LeftHand:这是左手腕骨
函数定义:HumanBodyBones.LeftHand
RightHand:这是右手腕骨
函数定义:HumanBodyBones.RightHand
LeftToes:这是左脚趾骨
函数定义:HumanBodyBones.LeftToes;
RightToes:这是右脚趾骨
函数定义:HumanBodyBones.RightToes;
LeftEye:这是左眼骨
函数定义:HumanBodyBones.LeftEye;
RightEye:这是右眼骨
函数定义:HumanBodyBones.RightEye;
Jaw:这是颚骨
函数定义:HumanBodyBones.Jaw
LeftThumbProximal:这是左手拇指第一指骨
函数定义:HumanBodyBones.LeftThumbProximal
LeftThumbIntermediate:这是左手拇指第二指骨
函数定义:HumanBodyBones.LeftThumbIntermediate
LeftThumbDistal:这是左拇指第三指骨
函数定义:HumanBodyBones.LeftThumbDistal
LeftIndexProximal:这是左指数第一指骨
函数定义:HumanBodyBones.LeftIndexProximal
LeftIndexIntermediate:这是左指数第二指骨
函数定义:HumanBodyBones.LeftIndexIntermediate
LeftIndexDistal:这是左指数第三指骨
函数定义:HumanBodyBones.LeftIndexDistal
LeftMiddleProximal:这是左中间的第一个指骨
函数定义:HumanBodyBones.LeftMiddleProximal
LeftMiddleIntermediate:这是左中间第二指骨
函数定义:HumanBodyBones.LeftMiddleIntermediate
LeftMiddleDistal:这是左中间的第三指骨
函数定义:HumanBodyBones.LeftMiddleDistal;
LeftRingProximal:这是左环第一指骨
函数定义:HumanBodyBones.LeftRingProximal
LeftRingIntermediate:这是左环第二指骨
函数定义:HumanBodyBones.LeftRingIntermediate
LeftRingDistal:这是左环第三指骨
函数定义:HumanBodyBones.LeftRingDistal
LeftLittleProximal:这是左边小指的第一指骨
函数定义:HumanBodyBones.LeftLittleProximal
LeftLittleIntermediate:这是左边小指的第二指骨
函数定义:HumanBodyBones.LeftLittleIntermediate
LeftLittleDistal:这是左边小指的第三指骨
函数定义:HumanBodyBones.LeftLittleDistal
RightThumbProximal:这是右手拇指的第一指骨
函数定义:HumanBodyBones.RightThumbProximal
RightThumbIntermediate:这是右手拇指的第二指骨
函数定义:HumanBodyBones.RightThumbIntermediate
RightThumbDistal:这是右手拇指的第三指骨
函数定义:HumanBodyBones.RightThumbDistal
RightIndexProximal:这是右指数第一指骨
函数定义:HumanBodyBones.RightIndexProximal
RightIndexIntermediate:这是右指数第二指骨
函数定义:HumanBodyBones.RightIndexIntermediate
RightIndexDistal:这是右指第三指骨
函数定义:HumanBodyBones.RightIndexDistal
RightMiddleProximal:这是右中间第一指骨
函数定义:HumanBodyBones.RightMiddleProximal
RightMiddleIntermediate:这是右中间第二指骨
函数定义:HumanBodyBones.RightMiddleIntermediate
RightMiddleDistal:这是右中间第三指骨
函数定义:HumanBodyBones.RightMiddleDistal
RightRingProximal:这是右环第一指骨
函数定义:HumanBodyBones.RightRingProximal
RightRingIntermediate:这是右环第二指骨
函数定义:HumanBodyBones.RightRingIntermediate
RightRingDistal:这是右环第三指骨
函数定义:HumanBodyBones.RightRingDistal
RightLittleProximal:这是右边小指的第一指骨
函数定义:HumanBodyBones.RightLittleProximal
RightLittleIntermediate:这是右边小指的第二指骨
函数定义:HumanBodyBones.RightLittleIntermediate
RightLittleDistal:这是右边小指的第三指骨
函数定义:HumanBodyBones.RightLittleDistal
LastBone:这是Last骨骼索引分隔符
函数定义:HumanBodyBones.LastBone;

二十四、AnimatorClipInfo
1.描述
  由Animator播放和混合的clip信息
Properties

clip:返回Animator播放的animation clip
函数定义:public AnimationClip clip;
代码示例:
using UnityEngine;

public class AnimationEventExample : MonoBehaviour
{
    Animator m_Animator;
    AnimatorClipInfo[] m_AnimatorClipInfo;
    private void Start()
    {
        m_Animator = GetComponent();
        m_AnimatorClipInfo = m_Animator.GetCurrentAnimatorClipInfo(0);
        Debug.Log("Starting clip : " + m_AnimatorClipInfo[0].clip);
    }
}
weight:返回Animator用于混合此clip的混合权重。
函数定义:public float weight;

二十五、AnimatorStateInfo
1.描述
  有关当前或下一个状态的信息
2.Properties

fullPathHash:此状态的完整路径的hash
函数定义:public int fullPathHash
length:该状态的当前持续时间。以秒为单位当State包含Blend Tree时可能会有所不同。
函数定义:public float length;
loop:状态是否循环。这个状态内的所有动画都必须循环播放。
函数定义:public bool loop;
normalizedTime:状态的normalized时间。整数部分是State循环的次数,小数部分是当前循环中的进度(0-1)。
函数定义:public float normalizedTime;
shortNameHash:使用Animator.StringToHash生成Hash.Hash不包括父Layer的名称
函数定义:public int shortNameHash;
speed:动画的播放速度。1是正常播放速度。负回放速度将从最后播放动画。
函数定义:public float speed。
speedMultiplier:这个状态的速度倍增器。负速度倍增器将向后播放动画。如果没有为此AnimatorState设置的speed parameter,则默认值为1。
函数定义:public float speedMultiplier;
tagHash:State的标签。可以由Animator.StringToHash生成
函数定义:public int tagHash;

3.Public Methods

IsName:名称是否与状态机中的激活状态名称匹配。名称应采用Layer.Name的形式,例如“Base.Idle”。
函数定义:public bool IsName(string name);
IsTag:Tag是否与状态机中的激活状态的Tag匹配。
函数定义:public bool IsTag(string tag);

二十六、AvatarIKHint
1.描述
  IK Hint,用于设置和获取IK权重和位置
2.Properties

LeftKnee:左膝IK hint
函数定义:AvatarIKHint.LeftKnee
RightKnee:右膝IK hint
函数定义:AvatarIKHint.RightKnee
LeftElbow:左肘IK hint
函数定义:AvatarIKHint.LeftElbow
RightElbow:右肘IK hint
函数定义:AvatarIKHint.RightElbow

二十七、AvatarIKGoal
1.描述
  IK Goal,用于设置和获取IK权重,位置和旋转
2.Properties

LeftFoot:左脚
函数定义:AvatarIKGoal.LeftFoot
RightFoot:右脚
函数定义:AvatarIKGoal.RightFoot
LeftHand:左手
函数定义:AvatarIKGoal.LeftHand
RightHand:右手
函数定义:AvatarIKGoal.RightHand

二十八、AvatarTarget
1.Properties

Root:root,游戏对象的位置
函数定义:AvatarTarget.Root
Body:身体,质心
函数定义:AvatarTarget.Body
LeftFoot:左脚
函数定义:AvatarTarget.LeftFoot
RightFoot:右脚
函数定义:AvatarTarget.RightFoot
LeftHand:左手
函数定义:AvatarTarget.LeftHand
RightHand:右手
函数定义:AvatarTarget.RightHand

二十九、MatchTargetWeightMask
1.描述
  使用此结构指定Animator.MatchTarget的position和rotation的权重掩码。
2.Properties

positionXYZWeight:Position XYZ的权重
函数定义:public Vector3 positionXYZWeight;
rotataionWeight:Rotation的权重
函数定义:public float rotationWeight

3.Constructors

MatchTargetWeightMask:MatchTargetWeightMask构造函数。
函数定义:public MatchTargetWeightMask(Vector3 positionXYZWeight,float rotationWeight);
函数参数:
positionXYZWeight:Position XYZ的权重
rotationWeight:Rotation 的权重

三十、AnimatorOverrideController
1.描述
  控制Animator Override Controller的接口。
  Animator Override Controller用于从控制器覆盖Animation Clip以专门给定Avatar的动画。在运行时使用基于相同AnimatorController的AnimatorOverrideController交换Animator.runtimeAnimatorController不会重置状态机的当前状态。
  有三种方法可以使用Animator Override Controller

1.在编辑器中创建Animator Override Controller
2.在运行时每帧更改一个动画片段(基本用例)。在这种情况下,可以使用索引器操作符AnimatorOverrideController.this[string],但要小心,因为每次调用都会触发Animator重新分配绑定的clip。
代码示例:
using UnityEngine;

public class AnimatorOverrideControllerExample : MonoBehaviour
{
    public AnimationClip[] weaponAnimationClip;
    protected Animator animator;
    protected AnimatorOverrideController animatorOverrideController;

    protected int weaponIndex;

    public void Start()
    {
        animator = GetComponent();
        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.在运行时每帧更改更多Animation Clip(高级用例)。AnimatorOverrideController.ApplyOverrides方法适用于此情况,因为它将重新分配绑定的Animation Clip的调用次数减少到一次。
代码示例:
using UnityEngine;
using System.Collections.Generic;

public class AnimationClipOverrides : List>
{
    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(this[index].Key, value);
        }
    }
}

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

public class AnimatorOverrideControllerExample : MonoBehaviour
{
    public Weapon[] weapons;
    protected Animator animator;
    protected AnimatorOverrideController animatorOverrideController;
    protected int weaponIndex;
    protected AnimationClipOverrides clipOverrides;

    public void Start()
    {
        animator = GetComponent();
        weaponIndex = 0;
        animatorOverrideController = new AnimatorOverrideController(animator.runtimeAnimatorController);
        animator.runtimeAnimatorController = animatorOverrideController;
        clipOverrides = new AnimationClipOverrides(animatorOverrideController.overridesCount);
        animatorOverrideController.GetOverrides(clipOverrides);
    }
    public void Update()
    {
        if(Input.GetButton("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);
        }
    }
}

2.Properties

overridesCount:返回overrides的计数
函数定义:public int overridesCount;
代码示例:
using UnityEngine;
using System.Collections.Generic;

public class AnimatorOverrideControllerExample : MonoBehaviour
{
    public AnimatorOverrideController overrideController;
    protected List> overrides;

    public void Start()
    {
        overrides = new List>(overrideController.overridesCount);
        overrideController.GetOverrides(overrides);
        for (int i = 0; i < overrides.Count; ++i)
        {
            Debug.Log("this is AnimatorOverrideController " + overrides[i].Key.name + ":" + overrides[i].Value.name);
            overrides[i] = new KeyValuePair(overrides[i].Key, null);
        }
    }
}
runtimeAnimatorController:Animator Override Controller覆盖的Runtime Animator Controller。注意:Animator Override Controllers不能嵌套,这意味着你无法为AnimatorOverrideController.runtimeAnimatorController提供Animator Override Controller。
函数定义:public RuntimeAnimatorController runtimeAnimatorController
this[string]:返回覆盖的Animation Clip(如果已设置)或原始的Animation Clip(名字为name)。注意:避免每个Animator每帧调用此函数一次以上,因为每次调用都会触发Animator绑定的clip重新分配。使用AnimatorOverrideController.ApplyOverrides代替。
函数定义:
public AnimationClip this[string]
public AnimationClip this[AnimationClip]

3.Constructors

AnimatorOverrideController:创建一个空的Animator Override Controller。
函数定义:
public AnimatorOverrideController():创建一个空的Animator Override Controller
public AnimatorOverrideController(RuntimeAnimatorController controller):创建一个覆盖控制器的Animator Override Controller
函数参数:
controller:要覆盖的Runtime Animator Controller
代码示例:尽管Animator Override Controller不支持嵌套的Animator Override Controller,但此构造函数将为你找到合适的控制器。
using UnityEngine;
using System.Collections.Generic;

public class AnimatorOverrideControllerExample : MonoBehaviour
{
    protected Animator animator;
    protected AnimatorOverrideController animatorOverrideController;
    public void Start()
    {
        animator = GetComponent();
        animatorOverrideController = new AnimatorOverrideController(animator.runtimeAnimatorController);
        animator.runtimeAnimatorController = animatorOverrideController;
    }
}

4.Public Methods

ApplyOverrides:在此Animator Override Controller上应用覆盖列表。只要你需要在同一帧中覆盖两个以上的动画片段,就应该使用此函数。该函数将通知Animator在处理完整个列表后更新所有内部绑定。
函数定义:
public void ApplyOverrides(IList>overrides);
函数参数:
overrides:要应用的覆盖列表
GetOverrides:获取当前在此Animator Clip overrides中定义的clip覆盖列表。如果使用AnimatorOverrideController.overridesCount预先分配覆盖列表,则此函数是无分配的。
函数定义:
public void GetOverrides(List> overrides)
函数参数:
overrides:用于接收结果的数组
代码示例:
using UnityEngine;
using System.Collections.Generic;

public class AnimatorOverrideControllerExample : MonoBehaviour
{
    public AnimatorOverrideController overrideController;
    protected List> overrides;
    public void ResetAllOverrides()
    {
        overrides = new List>(overrideController.overridesCount);
        overrideController.GetOverrides(overrides);
        for (int i = 0; i < overrides.Count; ++i)
            overrides[i] = new KeyValuePair(overrides[i].Key, null);
        overrideController.ApplyOverrides(overrides);
    }
}

5.Inherited Members
(1).Properties

Object.hideFlags:隐藏对象,使用场景保存还是用户可修改。
Object.name:对象的名称。
RuntimeAnimatorController.animationClips:检索控制器使用的所有AnimationClip
函数定义:public AnimationClip[] animationClips

(2).Public Methods

Object.GetInstanceID:返回对象的实例ID。始终保证对象的实例ID是唯一的。
Object.ToString:返回GameObject的名称

(3).Static Methods

Object.Destroy:删除GameObject、Component或者asset。
Object.DestroyImmediate:立即销毁对象obj。强烈建议你使用Destroy。
Object.DontDestroyOnLoad:加载新场景时,请勿销毁目标对象。
Object.FindObjectOfType:返回Type类型的第一个激活的加载对象。
Object.FindObjectsOfType:返回Type类型的所有激活的已加载对象的列表。
Object.Instantiate:克隆原始对象,并返回克隆的对象。

(4).Operators

Object.bool:对象是否存在
Object.operator !=:比较两个对象是否引用不同的对象
Object.operator ==:比较两个对象引用以查看它们是否引用同一对象

三十一、AnimatorUtility
1.描述
  Animator操作的各种实用程序
2.Static Methods

DeoptimizeTransformHierarchy:此函数将在GameObject下重新创建所有transform hierarchy。在运行时调用此函数将重新初始化Animator。(用于屏蔽OptimizeTransformHierarchy骨骼优化)
函数定义:public static void DeoptimizeTransformHierarchy(GameObject go);
函数参数:
go:Deoptimize的GameObject
OptimizeTransformHierarchy:此函数将删除GameObject下的所有变化层次结构,Animator将直接变换矩阵写入skin mesh矩阵,从而节省了大量的CPU周期。你可以选择提供transform名称列表,此函数将在GameObject下创建这些转换的平面hierarchy。在运行时调用此函数将重新初始化Animator。
函数定义:public static void OptimizeTransformHierarchy(GameObject go,string[] exposedTransforms)
函数参数:
go:要优化的GameObject
exposedTransforms:要公开的transform名称列表。

参考资料
【Unity 骨骼节点对象优化,AnimatorUtility.OptimizeTransformHierarchy】http://bubuko.com/infodetail-2924367.html
【Unity非运行模式下实现动画播放/回退工具】https://www.jianshu.com/p/3f62e8c56953
【动画系统中开启Optimize Game Object选项和使用Ragdoll系统存在冲突】https://answer.uwa4d.com/question/5af139a20e95a527a7a81cf4

你可能感兴趣的:(unity)