动画组件目前有新旧两种
旧版组件:Animation
我们可以自己简单制作一个动画
动画就是改变物体的数值我们可以添加属性来进行声明改变
添加后,就给了我们两帧,第一帧是什么样,第二帧改变成什么样,这样就产生了动画(可以通过滚轮来改变时间长度,以增加帧时间长)
使用脚本播放动画列表里的动画
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AnimationTest : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0))
{
//模仿动画片段
//GetComponent<Animation>().Play();
//从动画列表里找寻属于改名字的动画片段
GetComponent<Animation>().Play("right");
}
}
}
新版组件:Animator
新版动画组件,没有动画片段,而是控制器
如何添加控制器
新版创建动漫大致与上面一样,不过要注意创建新版动画时要选中挂在着新动画组件的物体,这样创建的动画是新动画与原来的动画不一致
新动画
旧动画
我们创建了两个动画,但在只有一个控制器,那么动画在哪里找到
点击控制器,会弹出一个弹框,动画就在这里面
这个方块中含有我们的动画,我们一般叫这个方块为“动画状态”。(一个控制器里放着多个动画状态,每一个动画状态包含一个动画文件和动画文件的设置)
脚本:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AnimatorTest : MonoBehaviour
{
private Animator animator;
// Start is called before the first frame update
void Start()
{
//获得动画器组件
animator = GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0))
{
//按下鼠标左键动画切换成newRight
animator.Play("newRight");
}
}
}
一般来说我们不会通过这样的方式来做动画的切换,这个只是演示,一般我们是通过过渡来实现动画的切换