unity序列帧的实现。包括三种效果,后续再补充

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

效果1:从头播放到尾
效果2:重复播放
效果3:正放、倒放交替播放

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class XuLieZhen : MonoBehaviour
{
    [Header("序列帧路径")]
    public string Path;
    [Header("更换间隔")]
    public float time = 0.01f;
    float delTime;
    [Header("是否循环播放")]
    bool IsLoop;
    [Header("从第几张开始循环")]
    public int LoopIndex;
    [Header("来回效果,勾选isloop无效")]
    bool IsPingPang;
    int index = 0;
    List<Sprite> sprites = new List<Sprite>();
    bool IsPlay;
    bool iscanPingPang;//记录初始状态
    public enum LoopModle
    {
        NoLoop, CommonLoop, PingPang
    }
    public LoopModle loopPattern = new LoopModle();
    /// <summary>
    /// 开始读取
    /// </summary>
    private void Awake()
    {
        delTime = time;
        Resources.UnloadUnusedAssets();
        for (int i = 0; i < Resources.LoadAll<Sprite>(Path).Length; i++)
        {
            sprites.Add(Resources.LoadAll<Sprite>(Path)[i]);
        }
        iscanPingPang = IsPingPang;
    }
    /// <summary>
    /// 开始时 初始化状态
    /// </summary>
    private void OnEnable()
    {
        IsPingPang = iscanPingPang;
        //if (!IsPingPang) IsPlay = true;
        //if (IsPingPang) StartCoroutine("PingPang");
        switch (loopPattern)
        {
            case LoopModle.NoLoop:
                IsPlay = true;
                IsLoop = false;
                break;
            case LoopModle.CommonLoop:
                IsPlay = true;
                IsLoop = true;
                break;
            case LoopModle.PingPang:
                IsPingPang = true;
                StartCoroutine("PingPang");
                break;
        }
    }
    /// <summary>
    /// 结束时 初始化状态
    /// </summary>
    private void OnDisable()
    {
        index = 0;
        transform.GetComponent<Image>().sprite = sprites[0];
    }
    void Update()
    {
        if (IsPlay) ChangePictures(IsLoop);
    }
    /// <summary>
    /// 播放序列帧
    /// </summary>
    /// <param name="isLoop">是否循环播放</param>
    void ChangePictures(bool isLoop)
    {
        time -= Time.deltaTime;
        if (time < 0)
        {
            transform.GetComponent<Image>().sprite = sprites[index];
            time = delTime;
            index++;
            if (index >= sprites.Count)
            {
                if (isLoop)
                {
                    index = LoopIndex;
                }
                else
                {
                    index = sprites.Count - 1;
                    IsPlay = false;
                }
            }
        }
    }
    /// <summary>
    /// pingpang效果
    /// </summary>
    /// <returns></returns>
    IEnumerator PingPang()
    {
        transform.GetComponent<Image>().sprite = sprites[index];
        yield return new WaitForSeconds(delTime);
        if (IsPingPang)
        {
            index++;
            if (index > sprites.Count - 1)
            {
                index = sprites.Count - 1;
                IsPingPang = false;
            }
        }
        else
        {
            index--;
            if (index < LoopIndex)
            {
                index = LoopIndex;
                IsPingPang = true;
            }
        }
        StopCoroutine("PingPang");
        StartCoroutine("PingPang");
    }
}

代码很简单,怎么使用直接看代码就能明白。关键是要填好面板上的信息。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值