UGUI—UISpriteAnimation

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

示例

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
/// <summary>
/// 精灵动画
/// </summary>
public class UISpriteAnimation : MonoBehaviour
{
    public Image image;
    public List<Sprite> sprites;
    [SerializeField]
    [Range(1, 60)]
    private int frameRate = 30; //帧率

    private float deltaTime;
    private float t;
    private int index;

    void Awake()
    {
        deltaTime = 1f / (float)frameRate;
    }

    void Update()
    {
        t += Time.deltaTime;
        if (t < deltaTime)
            return;

        t = 0;
        index = (index + 1) % sprites.Count;
        image.sprite = sprites[index];
    }

    public void SetFrameRate(int frameRate)
    {
        if (frameRate <= 0)
            frameRate = 1;
        if (frameRate > 60)
            frameRate = 60;
        deltaTime = 1f / (float)frameRate;
        this.frameRate = frameRate;
    }
}

标签: Unity3d

Powered by emlog  蜀ICP备18021003号   sitemap

川公网安备 51019002001593号