UGUI—UISpriteAnimation

作者:追风剑情 发布于:2019-12-2 11:32 分类:Unity3d

示例

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Sprites;
using UnityEngine.U2D;
using UnityEngine.UI;
/// <summary>
/// UI精灵动画
/// </summary>
public class UISpriteAnimation : MonoBehaviour
{
    [SerializeField]
    private Image m_Image;
    [SerializeField]
    private SpriteAtlas m_SpriteAtlas = null; //精灵图集
    [SerializeField]
    private string m_Prefix = string.Empty; //精灵前缀
    [SerializeField]
    private Sprite[] m_SpriteList = null; //精灵数组
    [SerializeField]
    private float[] m_FrameDuration; //控制每帧停顿时间
    [SerializeField]
    [Range(1, 60)]
    private int m_FrameRate  = 30; //帧率
    private float m_Interval = 0f;
    private int index = 0;
    private float t = 0;

    private void Awake()
    {
        m_Interval = 1.0f / (float)m_FrameRate;
    }

    private void Update()
    {
        t += Time.deltaTime;
        if (t < m_Interval)
            return;
        //控制帧暂停
        if (m_FrameDuration != null && index < m_FrameDuration.Length && t < m_FrameDuration[index])
            return;
        t = 0;

        if (m_SpriteAtlas != null)
        {
            index = (index + 1) % m_SpriteAtlas.spriteCount;
            Sprite sprite = m_SpriteAtlas.GetSprite(m_Prefix + index);
            m_Image.sprite = sprite;
        }
        else if (m_SpriteList != null)
        {
            index = (index + 1) % m_SpriteList.Length;
            Sprite sprite = m_SpriteList[index];
            if (sprite != null)
                m_Image.sprite = sprite;
        }
    }
}

标签: Unity3d

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号