コガネブログ

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

【Unity】時限式のコールバックを実装できる「TeaTime」紹介

はじめに

「TeaTime」を Unity プロジェクトに導入することで
時限式のコールバックを実装できるようになります

使用例

using matnesis.TeaTime;
using UnityEngine;

public class Example : MonoBehaviour
{
    private void Awake()
    {
        // 2 秒後に実行
        this
            .tt()
            .Add( 2, () => Debug.Log( "ピカチュウ" ) )
        ;

        // 3 秒毎に繰り返す
        this.tt()
            .Add( () => Debug.Log( "ピカチュウ" ) )
            .Add( 3 )
            .Repeat()
        ;
        
        // 1.5 秒間繰り返す
        this
            .tt()
            .Loop( 1.5f, _ => Debug.Log( "ピカチュウ" ) )
        ;

        // 自動実行を無効化
        var somethingForLater = this
            .tt()
            .Pause()
            .Add( 3, () => Debug.Log( "ピカチュウ" ) )
        ;

        somethingForLater.Play();

        // Tween ライブラリのように使用
        this
            .tt()
            .Add( () =>
            {
                transform.position = new Vector3( 999, 999, 999 );
            } )
            .Loop( 4, loop =>
            {
                transform.position = Vector3.Lerp
                (
                    transform.position,
                    Vector3.zero,
                    loop.deltaTime
                );
            } )
            .Add( () => Debug.Log( "ピカチュウ" ) )
        ;
    }
}

関連記事