The use of Unity Animation and Animator (super detailed)

1. Add animation

Select the object to be animated, under the Animation window
Insert image description hereNotice: If the selected object does not have an Animation/Animator component, the Animator component will be added automatically.
Insert image description here

2. Animation

2.1 Production interface

Insert image description here
Insert image description herePreview: Enable/disable scene preview mode.

Insert image description hereRecording: Enable/disable keyframe recording mode.

Insert image description hereGo to the beginning of the animation clip.

Insert image description hereGo to the previous keyframe.

Insert image description herePlay animation clip.

Insert image description hereGo to next keyframe.

Insert image description hereGo to the end of the animation clip.

Insert image description herecurrent frame.

Insert image description hereCurrent animation name, drop down to create a new Animation animation.

Insert image description hereSamples, frames per second/minute.

Insert image description hereAdd keyframes.

Insert image description hereAdd event.

Insert image description hereTimeline.

Insert image description hereControl deletion of attributes or increase or decrease keys.

Insert image description herebriefing.

Insert image description herecurve.

2.2 The produced Animation animation

Insert image description here

Length Length (animation duration)
Loop Time cycle
Loop Pose Loop action (make the head and tail connection smooth when looping)
Cycle Offset Smoothness

2.3 Adding and using events

Insert image description here

public class AnimEvents : MonoBehaviour
{
    
    

    private void Start()
    {
    
    
        // 创建一个事件
        AnimationEvent evt = new AnimationEvent();
        // 绑定触发事件后要执行的方法名
        evt.functionName = "PrintEvent";
        // 执行方法后要传入的参数
        evt.intParameter = 12345;
        // 设置事件关键帧的位置,当事件过了1.3秒后执行
        evt.time = 0.5f;
        // 设置目标动画剪辑
        AnimationClip clip = this.GetComponent<Animator>().runtimeAnimatorController.animationClips[0];
        // 绑定事件
        clip.AddEvent(evt);
    }

    private void TestAniEvent(string param)
    {
    
    
        Debug.Log("事件触发,参数是:" + param);
    }

    private void PrintEvent(int param)
    {
    
    
        Debug.Log(param);
    }
}

==Note:==The script and the object where the animator is located are in the same object

3. Animator

3.1 Production interface

Insert image description here

3.2 Explanation of some parameters

  1. Solo and Mute
    When an animation has multiple subsequent animations:
    Insert image description here
    After the animation is played, the front (or top) state transition in the action list is given priority.
    Solo: When a certain Transition is set to Solo, the mark is given priority. Solo's animation transfer
    Mute: selected state transfer is disabled
  2. ** Has Exit Time **
    Whether there is an exit time. Simple understanding: turning on means waiting for the current animation to finish before proceeding to the next animation; turning off means interrupting the current animation immediately and playing the next animation.

3.3 Animation parameters

Insert image description here
Insert image description here
Code control:

public class AnimControl : MonoBehaviour
{
    
    
    private Animator anim;
    private void Start()
    {
    
    
        anim = this.GetComponent<Animator>();
    }

    private void Update()
    {
    
    
        if (Input.GetKeyDown(KeyCode.R))
        {
    
    
            anim.SetBool("BoolParams", true);
        }
        if (Input.GetKeyDown(KeyCode.S))
        {
    
    
            anim.SetBool("BoolParams", false);
        }
    }
}

Insert image description here

4. Related classes, properties, and APIs in Animator

4.1 Class

AnimationClip: animation clip, animation

Properties/Methods meaning
length Animation length in seconds. (read only)
frameRate The frame rate at which keyframes are sampled. (read only)
length / (1 / frameRate) Total frame rate
AddEvent Add an animation event to the clip

AnimatorStateInfo: animation state machine status information

Properties/Methods meaning
fullPathHash The full path hash of the state
length The current duration of the state
normalizedTime The integer part is the number of times the state has been cycled. The decimal part is the progress percentage of the current loop (0-1)
speed The playback speed of this animation. 1 means normal playback speed

4.2 Properties

Attributes meaning
speed The playback speed of the animator. 1 is the normal playback speed
runtimeAnimatorController.animationClips Return all animationsAnimationClip[]

4.3 API

method name meaning
GetBool Returns the value of the given boolean argument
GetFloat Returns the value of the given float argument
GetInteger Returns the value of the given integer argument
SetBool Sets the value of the given boolean parameter
SetFloat Send floating point values ​​to animators to affect transitions
SetInteger Sets the value of the given integer argument
GetCurrentAnimatorClipInfo Returns an array of all AnimatorClipInfo's in the current state of the given layer
GetCurrentAnimatorClipInfoCount Returns the number of AnimatorClipInfo in the current state
GetCurrentAnimatorStateInfo Returns AnimatorStateInfo containing information about the current state
GetNextAnimatorClipInfo Returns an array of all AnimatorClipInfos in the next state of the given layer
GetNextAnimatorClipInfoCount Returns the number of AnimatorClipInfo in the next state
GetNextAnimatorStateInfo Returns AnimatorStateInfo containing information about the next state
Play play a state

4.4 Several key methods

Animator.Play

void Play(string stateName, int layer = -1, float normalizedTime = float.NegativeInfinity);
Parameters Description Description
stateName The name of the animation state to be played.
layer The layer the animation state is on.
normalizedTime The normalized time (0-1, time scale value) of the animation state to be played.

5. Animation playback and pause control

1. Animotor

// 播放
animator.Play("ani_name");
// 暂停
animator.speed = 0;
// 继续播放
animator.speed = 1;
//  重置到起始帧
animator.Play("ani_name", 0, 0f);

2. Animation

// 播放
animition.Play("ani_name");
// 暂停
animition["ani_name"].speed = 0;
// 继续播放
animition["ani_name"].speed = 1;
// 重置到起始帧
public void ResetAni(Animation ani, string name)
{
    
    
     AnimationState state = ani[name];
     ani.Play(name);
     state.time = 0;
     ani.Sample();
     state.enabled = false;
 }

Guess you like

Origin blog.csdn.net/weixin_45136016/article/details/132756008