1、导入带动画模型(卡吊车02)

2、在模型(卡吊车02)添加animation组件,并拖动赋值动画(Take 001)

注意点1:点击动画(Take 001)在debug模式下勾选legace选项


注意点2:如果动画(Take 001)不能进入debug模式,可将动画(Take 001)复制出来,修改后在拖拽给第2步中的animation。
3、动画(Take 001)播放进度控制脚本(anima_play_controller):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class anima_play_controller : MonoBehaviour
{
public Animation anim;
private float timeRecd;
private int index=0;
/// <summary>
/// animation控制:
/// 空格键:暂停、恢复
/// 下:播放
/// 上:停止
/// 左:倒放
/// </summary>
void Update()
{
//播放
if (Input.GetKeyDown(KeyCode.DownArrow))
{
Debug.Log("begin");
anim.Play("Take 001");
}
//停止
else if (Input.GetKeyDown(KeyCode.UpArrow))
{
Debug.Log("stop");
anim.Stop();
}
//倒放
else if (Input.GetKeyDown(KeyCode.UpArrow))
{
Debug.Log("back");
anim["Take 001"].speed = -1;
}
//暂停/恢复
else if (Input.GetKeyDown(KeyCode.Space))
{
if (index == 0)
{
Debug.Log("pause");
anim["Take 001"].speed = 0;
Debug.Log(anim["Take 001"].normalizedTime);
index = 1;
}
else if (index == 1)
{
Debug.Log("resume");
anim["Take 001"].speed = 1;
index = 0;
}
}
//暂停/恢复
/*
* else if (Input.GetKeyDown(KeyCode.P))
{
Debug.Log("pause");
anim["Take 001"].speed = 0;
//timeRecd = anim["Take 001"].time;
//anim.Stop();
}
else if (Input.GetKeyDown(KeyCode.R))
{
Debug.Log("resume");
anim["Take 001"].speed = 1;
//anim["Take 001"].time = timeRecd;
//anim.Play("Take 001");
}
*/
}
}
4、添加空物体,命名为animation,并添加控制脚本(anima_play_controller),
将模型(卡吊车02)拖拽复制给脚本公共变量Anima(实际上是拖拽模型下的动画组件animation):

5、演示:



这篇博客介绍了如何在Unity中实现动画播放进度的控制。首先,通过导入带有动画的模型并添加animation组件来设置动画。在调试模式下确保动画启用legace选项。如果无法进入调试模式,可以复制并修改动画后再赋值。接着,创建一个控制脚本(anima_play_controller),将动画组件赋予脚本的公共变量Anima,以实现动画播放控制。
2万+

被折叠的 条评论
为什么被折叠?



