Article directory
1. Add animation
Select the object to be animated, under the Animation window
Notice: If the selected object does not have an Animation/Animator component, the Animator component will be added automatically.
2. Animation
2.1 Production interface
Preview: Enable/disable scene preview mode.
Recording: Enable/disable keyframe recording mode.
Go to the beginning of the animation clip.
Go to the previous keyframe.
Play animation clip.
Go to next keyframe.
Go to the end of the animation clip.
current frame.
Current animation name, drop down to create a new Animation animation.
Samples, frames per second/minute.
Add keyframes.
Add event.
Timeline.
Control deletion of attributes or increase or decrease keys.
briefing.
curve.
2.2 The produced Animation animation
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
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
3.2 Explanation of some parameters
- Solo and Mute
When an animation has multiple subsequent animations:
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 - ** 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
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);
}
}
}
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;
}