using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System;
[Serializable]
public struct SpriteInfo
{
public Image sourceImage;
public Sprite[] textureArray;
public float frameRate;
}
public class SpriteAnimation : MonoBehaviour
{
[SerializeField] SpriteInfo s_Info;
[SerializeField] float frameTimer;
[SerializeField] int currentFrame;
[SerializeField] int frameCount;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown("1"))
{
StartCoroutine(StartSprite(s_Info));
}
}
IEnumerator StartSprite(SpriteInfo info)
{
frameTimer = info.frameRate;
frameCount = info.textureArray.Length;
//Rect rect = new Rect(0, 0, info.textureArray[0].width, info.textureArray[0].height);
while (true)
{
frameTimer -= Time.deltaTime;
if (frameTimer <= 0f)
{
frameTimer += info.frameRate;
currentFrame = (currentFrame + 1) % frameCount;
info.sourceImage.sprite = info.textureArray[currentFrame];
Debug.Log(info.sourceImage.sprite.name);
//info.sourceImage.sprite = Sprite.Create(info.textureArray[currentFrame],rect,Vector2.zero);
}
yield return new WaitForEndOfFrame();
}
}
}
댓글 없음:
댓글 쓰기