facebook twitter hatena line email

「Unity/Imageアニメ」の版間の差分

提供: 初心者エンジニアの簡易メモ
移動: 案内検索
行44: 行44:
 
         foreach (string tmpAnimationName in animationNames)
 
         foreach (string tmpAnimationName in animationNames)
 
         {
 
         {
 +
            // dirで画像を管理してる場合
 
             Sprite image = Resources.Load<Sprite>(tmpAnimationName);
 
             Sprite image = Resources.Load<Sprite>(tmpAnimationName);
 +
/*
 +
            // 画像をmultipleで分割してる場合
 +
            string[] name = tmpAnimationName.Split('/');
 +
            string fileName = name[0];
 +
            string fileSplitName = name[1];
 +
            Sprite[] spriteSplits = Resources.LoadAll<Sprite>(fileName);
 +
            Sprite image = System.Array.Find<Sprite>(spriteSplits, (sprite) => sprite.name.Equals(fileSplitName));
 +
*/
 
             sprites.Add(image);
 
             sprites.Add(image);
 
         }
 
         }

2021年12月16日 (木) 20:20時点における版

Imageを、アニメーション対応にする。

hoge/hoge_00.pngなどをResourcesのフォルダ内に入れておく。

SampleAnimationImage.cs

using System.Collections.Generic;
public class SampleAnimationImage : AbstractAnimationImage
{
    public void Init()
    {
        loop = true; // ループonのとき
        animationNames = new List<string>();
        animationNames.Add("hoge/hoge_00");
        animationNames.Add("hoge/hoge_01");
        animationNames.Add("hoge/hoge_02");
        animationNames.Add("hoge/hoge_03");
        animationNames.Add("hoge/hoge_04");
        animationNames.Add("hoge/hoge_05");
        animationNames.Add("hoge/hoge_06");
        base.Init();
    }
}

AbstractAnimationImage.cs

using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class AbstractAnimationImage : Image
{
    protected System.Diagnostics.Stopwatch sw;
    int framenum = 0;
    public float animeMs = 0f;
    List<Sprite> sprites;
    protected List<string> animationNames = new List<string>();
    public bool loop = false;

    protected void Init()
    {
        sprites = new List<Sprite>();
        sw = new System.Diagnostics.Stopwatch();
        sw.Start();
        foreach (string tmpAnimationName in animationNames)
        {
            // dirで画像を管理してる場合
            Sprite image = Resources.Load<Sprite>(tmpAnimationName);
/*
            // 画像をmultipleで分割してる場合
            string[] name = tmpAnimationName.Split('/');
            string fileName = name[0];
            string fileSplitName = name[1];
            Sprite[] spriteSplits = Resources.LoadAll<Sprite>(fileName);
            Sprite image = System.Array.Find<Sprite>(spriteSplits, (sprite) => sprite.name.Equals(fileSplitName));
*/
            sprites.Add(image);
        }
    }
    public void Play()
    {
        animeMs = sw.ElapsedMilliseconds;
        if (sprites.Count > framenum)
        {
            this.sprite = sprites[framenum];
            framenum++;
        }
        if (loop)
        {
            if (framenum >= sprites.Count)
            {
                framenum = 0;
            }
        }
    }
}

MainScene.cs

GameObject obj = new GameObject();
GameObject parent = GameObject.Find("/Canvas");
obj.transform.SetParent(parent.transform, false);
SampleAnimationImage animationImage = obj.AddComponent<SampleAnimationImage>();
animationImage.Init();
sw = new System.Diagnostics.Stopwatch();
sw.Start();
void Update()
{
    if (animationImage != null && sw != null)
    {
        if (animationImage.animeMs + 30f < sw.ElapsedMilliseconds)
        {
            animationImage.Play();
        }
    }
}