Unity的Amimation编辑器动画使用

版权声明:请尊重原劳动成果 https://blog.csdn.net/qq_39646949/article/details/86299006

Amimation动画可以重复播放某一段动画
1、设置
在这里插入图片描述
将带有动画的模型选择,如图所示,点击Apply。
2、将下面脚本挂在带有Amimation动画的Gameobject上
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class AnimationnPlay : MonoBehaviour {
private Animation ani;
private void Start()
{
ani = this.GetComponent();
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.W))
{
PlayAnimation(“Idle”, 1f);
}
}
private void PlayAnimation(string _name, float speed = 1f)
{
if (ani[_name] != null)
{
if (ani.isPlaying)
{
ani.Stop();
}
if (speed == -1)
{
ani[_name].time = ani[_name].length;
}
else
{
ani[_name].time = 0;
}
ani[_name].speed = speed;
print(ani[_name].speed + " : " + ani[_name].name + ": " + ani[_name].length);
ani.Play(_name);
}
else
{
print(“请输入动画片段名字”);
}
}
}
3、点一下W键,即可看到播放一下Idle动画片段。

4、补充一个,如果要让这一段动画倒着可以重复播放,怎么做?
PlayAnimation(“Idle”, -1f);
上面调用方法,改为-1f,即可。

猜你喜欢

转载自blog.csdn.net/qq_39646949/article/details/86299006
今日推荐