Unity 加载2D序列帧动画

1 篇文章 0 订阅
1 篇文章 0 订阅

利用Unity做2D游戏或者应用的时候,通常用到序列帧动画;
所需要播放的动画 通常有很多,所以构造一个动画类 来控制动画的播放会无比方便

以下是代码

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Events;

public class AniSprite
{
    private float speed;
    private Sprite[] sprites;
    private bool isPlay;
    private int indexSprte;
    private Image backImage;
    private float timer;

    //动画结束的事件
    public UnityAction EndAniVoid;


    /// <summary>
    /// 构造方法
    /// </summary>
    /// <param name="动画播放速度"></param>
    /// <param name="需要播放的序列帧"></param>
    /// <param name="需要播放的容器(image)"></param>
    public AniSprite(float _speed, Sprite[] _sprites, Image _backImage)
    {
        speed = _speed;
        sprites = _sprites;
        backImage = _backImage;
        isPlay = false;

    }

    public void Pause()
    {
        isPlay = false;
    }

    public void Restore()
    {
        isPlay = true;
    }


    public void Play()
    {

        indexSprte = 0;
        timer = 0;
        isPlay = true;
        //Debug.Log(isPlay);
        //Debug.Log(isPlay);
    }


    public void Update()
    {

        if (isPlay.Equals(true))
        {
           
            timer += Time.deltaTime;
            if (timer >= speed)
            {
              
                if (indexSprte >= sprites.Length)
                {
                    //Debug.Log("序列动画结束");
                    if (EndAniVoid != null)
                    {
                        EndAniVoid();
                    }

                    isPlay = false;
                    return;
                }
                backImage.sprite = sprites[indexSprte];
                indexSprte++;
                timer = 0;
            }

        }
    }

}

测试调用:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class testss : MonoBehaviour
{

    public Sprite[] testSprites;
    public Image testBackImage;
    AniSprite ani;
    // Start is called before the first frame update
    void Start()
    {


        ani = new AniSprite(0.02f, testSprites, testBackImage);
        ani.Play();
        ani.EndAniVoid += delegate () 
        {
            Debug.Log("动画播放结束");
        };
      
      
    }

    // Update is called once per frame
    void Update()
    {
        if (ani!=null)
        {
            ani.Update();
        }
    }
}

**注意:实例化动画类是,需要将类的 update方法 一直执行,如上图所示

利用此类我做了一个 适应于夜场的小游戏 效果展示

如果有想要此项目或者源码 亦或者想要深入了解的朋友可以与我联系

QQ:237603564

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qq_37114869

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值