unity序列帧动画
程序员文章站
2022-03-25 21:32:44
...
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AnimationTest : MonoBehaviour
{
//Sprite渲染器
private SpriteRenderer _sp;//也可private Image_img;相应代码都要修改一下,记得添加相应命名空间
//Sprite动画帧
public Sprite[] _clips;
//动画计时器
public float timer = 0.08f;
//当前帧数
protected int _frame=0;
private float time;
// Use this for initialization
void Start()
{
time = timer;
//为当前GameObject添加一个Sprite渲染器
_sp = this.gameObject.GetComponent<SpriteRenderer>();
//设置第一帧的Sprite
_sp.sprite = _clips[_frame];
}
// Update is called once per frame
void Update()
{
//更新时间
timer -= Time.deltaTime;
if (timer<=0)
{
timer = time;
if (_frame <_clips.Length-1)
{
//更新帧
_frame++;
//更新Sprite动画帧
_sp.sprite = _clips[_frame];
}
else
{
_frame = 0;
}
}
}
}