コガネブログ

平日更新を目標に Unity や C#、Visual Studio、ReSharper などのゲーム開発アレコレを書いていきます

【Unity】async / await で Tween を使用できる「AnimeTask」紹介

はじめに

「AnimeTask」を Unity プロジェクトに導入することで
async / await で Tween を使用できるようになります

使用例

f:id:baba_s:20181222175144g:plain

使い方

using AnimeTask;
using System.Threading.Tasks;
using UnityEngine;

public class Example : MonoBehaviour
{
    public GameObject m_gameObject;

    private async Task Start()
    {
        // ( -1, 0, 0 ) から ( 1, 0, 0 ) に 1 秒かけて Linear のイージングで移動
        await Anime.Play
        (
            Easing.Create<Linear>( new Vector3( -1, 0, 0 ), new Vector3( 1, 0, 0 ), 1 ),
            TranslateTo.LocalPosition( m_gameObject )
        );

        // 現在地から ( -1, 0, 0 ) に 1 秒かけて InCubic のイージングで移動
        await Anime.PlayTo
        (
            Easing.Create<InCubic>( new Vector3( -1, 0, 0 ), 1 ),
            TranslateTo.LocalPosition( m_gameObject )
        );

        // 0 から 1 の数値を 1 秒かけてログ出力
        await Anime.Play
        (
            Easing.Create<Linear>( 0, 1, 1 ),
            TranslateTo.Action<float>( c => Debug.Log( c ) )
        );
    }
}

上記のようなコードを記述することで使用できます