facebook twitter hatena line email

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

提供: 初心者エンジニアの簡易メモ
移動: 案内検索
行1: 行1:
destruction_air/destruction_air_00.pngなどをResourcesのフォルダ内に入れておく。
+
hoge/hoge_00.pngなどをResourcesのフォルダ内に入れておく。
  
AttackToonAnimation.cs
+
SampleAnimation.cs
 
<pre>
 
<pre>
 
using System.Collections.Generic;
 
using System.Collections.Generic;
public class AttackToonAnimation : AbstractAnimation
+
public class SampleAnimation : AbstractAnimation
 
{
 
{
    string animationNameAir = "destruction_air/destruction_air_";
 
 
     public void Init()
 
     public void Init()
 
     {
 
     {
 
         animationNames = new List<string>();
 
         animationNames = new List<string>();
         animationNames.Add("destruction_air/destruction_air_00");
+
         animationNames.Add("hoge/hoge_00");
         animationNames.Add("destruction_air/destruction_air_01");
+
         animationNames.Add("hoge/hoge_01");
         animationNames.Add("destruction_air/destruction_air_02");
+
         animationNames.Add("hoge/hoge_02");
         animationNames.Add("destruction_air/destruction_air_03");
+
         animationNames.Add("hoge/hoge_03");
         animationNames.Add("destruction_air/destruction_air_04");
+
         animationNames.Add("hoge/hoge_04");
         animationNames.Add("destruction_air/destruction_air_05");
+
         animationNames.Add("hoge/hoge_05");
         animationNames.Add("destruction_air/destruction_air_06");
+
         animationNames.Add("hoge/hoge_06");
 
         base.Init();
 
         base.Init();
 
     }
 
     }
行70: 行69:
 
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<AttackToonAnimation>();
+
animation = obj.AddComponent<SampleAnimation>();
 
animation.Init();
 
animation.Init();
 
sw = new System.Diagnostics.Stopwatch();
 
sw = new System.Diagnostics.Stopwatch();

2021年6月3日 (木) 04:38時点における版

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

SampleAnimation.cs

using System.Collections.Generic;
public class SampleAnimation : AbstractAnimation
{
    public void Init()
    {
        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();
    }
}

AbstractAnimation.cs

using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class AbstractAnimation : 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)
        {
            Sprite image = Resources.Load<Sprite>(tmpAnimationName);
            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);
animation = obj.AddComponent<SampleAnimation>();
animation.Init();
sw = new System.Diagnostics.Stopwatch();
sw.Start();
void Update()
{
    if (animation != null && sw != null)
    {
        if (animation.animeMs + 30f < sw.ElapsedMilliseconds)
        {
            animation.Play();
        }
    }
}