facebook twitter hatena line email

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

提供: 初心者エンジニアの簡易メモ
移動: 案内検索
 
(同じ利用者による、間の9版が非表示)
行1: 行1:
 +
Imageを、アニメーション対応にする。
 +
 
hoge/hoge_00.pngなどをResourcesのフォルダ内に入れておく。
 
hoge/hoge_00.pngなどをResourcesのフォルダ内に入れておく。
  
SampleAnimation.cs
+
SampleAnimationImage.cs
 
<pre>
 
<pre>
 
using System.Collections.Generic;
 
using System.Collections.Generic;
public class SampleAnimation : AbstractAnimation
+
public class SampleAnimationImage : AbstractAnimationImage
 
{
 
{
 
     public void Init()
 
     public void Init()
 
     {
 
     {
         animationNames = new List<string>();
+
         loop = true; // ループonのとき
         animationNames.Add("hoge/hoge_00");
+
        animationFrames = new List<AnimeFrame>();
         animationNames.Add("hoge/hoge_01");
+
         animationFrames.Add(new AnimeFrame("hoge/hoge_00", 200));
         animationNames.Add("hoge/hoge_02");
+
         animationFrames.Add(new AnimeFrame("hoge/hoge_01", 200));
         animationNames.Add("hoge/hoge_03");
+
         animationFrames.Add(new AnimeFrame("hoge/hoge_02", 200));
         animationNames.Add("hoge/hoge_04");
+
         animationFrames.Add(new AnimeFrame("hoge/hoge_03", 200));
         animationNames.Add("hoge/hoge_05");
+
         animationFrames.Add(new AnimeFrame("hoge/hoge_04", 200));
         animationNames.Add("hoge/hoge_06");
+
         animationFrames.Add(new AnimeFrame("hoge/hoge_05", 200));
 +
         animationFrames.Add(new AnimeFrame("hoge/hoge_06", 200));
 
         base.Init();
 
         base.Init();
 
     }
 
     }
 
}
 
}
 
</pre>
 
</pre>
AbstractAnimation.cs
+
AbstractAnimationImage.cs
 
<pre>
 
<pre>
 
using System.Collections.Generic;
 
using System.Collections.Generic;
 
using UnityEngine;
 
using UnityEngine;
 
using UnityEngine.UI;
 
using UnityEngine.UI;
public class AbstractAnimation : Image
+
public class AbstractAnimationImage : Image
 
{
 
{
 
     protected System.Diagnostics.Stopwatch sw;
 
     protected System.Diagnostics.Stopwatch sw;
行31: 行34:
 
     public float animeMs = 0f;
 
     public float animeMs = 0f;
 
     List<Sprite> sprites;
 
     List<Sprite> sprites;
     protected List<string> animationNames = new List<string>();
+
     protected List<AnimeFrame> animationFrames = new List<AnimeFrame>();
 
     public bool loop = false;
 
     public bool loop = false;
  
 +
    public class AnimeFrame
 +
    {
 +
        public string fileName = "";
 +
        public int ms = 1000; // 1secを1000とする
 +
 +
        public AnimeFrame(string fileName, int ms)
 +
        {
 +
            this.fileName = fileName;
 +
            this.ms = ms;
 +
        }
 +
    }
 
     protected void Init()
 
     protected void Init()
 
     {
 
     {
行39: 行53:
 
         sw = new System.Diagnostics.Stopwatch();
 
         sw = new System.Diagnostics.Stopwatch();
 
         sw.Start();
 
         sw.Start();
         foreach (string tmpAnimationName in animationNames)
+
         foreach (AnimeFrame tmpAnimationFrame in animationFrames)
 
         {
 
         {
             Sprite image = Resources.Load<Sprite>(tmpAnimationName);
+
            // dirで画像を管理してる場合
 +
             Sprite image = Resources.Load<Sprite>(tmpAnimationFrame.fileName);
 +
/*
 +
            // 画像をmultipleで分割してる場合
 +
            string[] name = tmpAnimationFrame.fileName.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);
 
         }
 
         }
 +
        // 初回表示
 +
        AnimeFrameView(0);
 
     }
 
     }
 +
    // 画像表示
 +
    void AnimeFrameView(int frame)
 +
    {
 +
        this.sprite = sprites[frame];
 +
    }
 +
    // 次の画像を出す
 
     public void Play()
 
     public void Play()
 
     {
 
     {
 
         animeMs = sw.ElapsedMilliseconds;
 
         animeMs = sw.ElapsedMilliseconds;
         if (sprites.Count > framenum)
+
         if (sprites.Count > framenum + 1)
 
         {
 
         {
             this.sprite = sprites[framenum];
+
             AnimeFrameView(framenum + 1);
 
             framenum++;
 
             framenum++;
 
         }
 
         }
         if (loop)
+
         else
 
         {
 
         {
             if (framenum >= sprites.Count)
+
             if (loop)
 
             {
 
             {
 +
                AnimeFrameView(0);
 
                 framenum = 0;
 
                 framenum = 0;
 
             }
 
             }
 
         }
 
         }
 +
    }
 +
    // 切り替えまでのフレームミリ秒数
 +
    public int GetFrameMs()
 +
    {
 +
        // 件数を超えてきたときは、ずっと待つ
 +
        if (framenum >= sprites.Count)
 +
        {
 +
            return int.MaxValue;
 +
        }
 +
        return animationFrames[framenum].ms;
 
     }
 
     }
 
}
 
}
行69: 行111:
 
GameObject parent = GameObject.Find("/Canvas");
 
GameObject parent = GameObject.Find("/Canvas");
 
obj.transform.SetParent(parent.transform, false);
 
obj.transform.SetParent(parent.transform, false);
animation = obj.AddComponent<SampleAnimation>();
+
SampleAnimationImage animationImage = obj.AddComponent<SampleAnimationImage>();
animation.Init();
+
animationImage.Init();
 
sw = new System.Diagnostics.Stopwatch();
 
sw = new System.Diagnostics.Stopwatch();
 
sw.Start();
 
sw.Start();
 
void Update()
 
void Update()
 
{
 
{
     if (animation != null && sw != null)
+
     if (animationImage != null && sw != null)
 
     {
 
     {
         if (animation.animeMs + 30f < sw.ElapsedMilliseconds)
+
         if (animationImage.animeMs + animationImage.GetFrameMs() < sw.ElapsedMilliseconds)
 
         {
 
         {
             animation.Play();
+
             animationImage.Play();
 
         }
 
         }
 
     }
 
     }
 
}
 
}
 
</pre>
 
</pre>

2021年12月17日 (金) 12:01時点における最新版

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

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

SampleAnimationImage.cs

using System.Collections.Generic;
public class SampleAnimationImage : AbstractAnimationImage
{
    public void Init()
    {
        loop = true; // ループonのとき
        animationFrames = new List<AnimeFrame>();
        animationFrames.Add(new AnimeFrame("hoge/hoge_00", 200));
        animationFrames.Add(new AnimeFrame("hoge/hoge_01", 200));
        animationFrames.Add(new AnimeFrame("hoge/hoge_02", 200));
        animationFrames.Add(new AnimeFrame("hoge/hoge_03", 200));
        animationFrames.Add(new AnimeFrame("hoge/hoge_04", 200));
        animationFrames.Add(new AnimeFrame("hoge/hoge_05", 200));
        animationFrames.Add(new AnimeFrame("hoge/hoge_06", 200));
        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<AnimeFrame> animationFrames = new List<AnimeFrame>();
    public bool loop = false;

    public class AnimeFrame
    {
        public string fileName = "";
        public int ms = 1000; // 1secを1000とする

        public AnimeFrame(string fileName, int ms)
        {
            this.fileName = fileName;
            this.ms = ms;
        }
    }
    protected void Init()
    {
        sprites = new List<Sprite>();
        sw = new System.Diagnostics.Stopwatch();
        sw.Start();
        foreach (AnimeFrame tmpAnimationFrame in animationFrames)
        {
            // dirで画像を管理してる場合
            Sprite image = Resources.Load<Sprite>(tmpAnimationFrame.fileName);
/*
            // 画像をmultipleで分割してる場合
            string[] name = tmpAnimationFrame.fileName.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);
        }
        // 初回表示
        AnimeFrameView(0);
    }
    // 画像表示
    void AnimeFrameView(int frame)
    {
        this.sprite = sprites[frame];
    }
    // 次の画像を出す
    public void Play()
    {
        animeMs = sw.ElapsedMilliseconds;
        if (sprites.Count > framenum + 1)
        {
            AnimeFrameView(framenum + 1);
            framenum++;
        }
        else
        {
            if (loop)
            {
                AnimeFrameView(0);
                framenum = 0;
            }
        }
    }
    // 切り替えまでのフレームミリ秒数
    public int GetFrameMs()
    {
        // 件数を超えてきたときは、ずっと待つ
        if (framenum >= sprites.Count)
        {
            return int.MaxValue;
        }
        return animationFrames[framenum].ms;
    }
}

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 + animationImage.GetFrameMs() < sw.ElapsedMilliseconds)
        {
            animationImage.Play();
        }
    }
}