昨天,一个妹子问我,Unity实现序列帧动画,然后说他们老师说要结合Animation,今天想了一下,一起进步,嘿嘿
1.序列帧图
boy.png
2.sprite editor
image.png
3.Animation
Animation
4.实现效果
Ani.gif
也看到很多大佬写的一些脚本实现,自己就简单代码实现一下
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
[RequireComponent(typeof(Image))]
public class ImgAni : MonoBehaviour
{
private Image ImageCom;
public List<Sprite> SpriteList;
private int spriteId = 0;
private float perTime = 0.2f;
private float curTime = 0;
public bool isLoop=false;
private bool keepLoop = true;
private void Awake()
{
ImageCom = GetComponent<Image>();
}
void Update()
{
if (SpriteList.Count == 0|| keepLoop == false)
{
return;
}
curTime += Time.deltaTime;
if (perTime < curTime)
{
if (spriteId > SpriteList.Count - 1)
{
spriteId = spriteId - SpriteList.Count;
if (!isLoop)
{
keepLoop = false;
}
}
ImageCom.sprite = SpriteList[spriteId];
spriteId++;
curTime = 0;
}
}
}
网友评论