コガネブログ

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

【Unity】エディタ拡張で async / await を使用する

概要

using System.Threading.Tasks;
using UnityEditor;
using UnityEngine;

public class Example
{
    [MenuItem( "Tools/Run" )]
    private static void Run()
    {
        RunAsync();
    }

    private static async void RunAsync()
    {
        var count = 10;

        for ( int i = 0; i < count; i++ )
        {
            var num = i + 1;

            Debug.Log( $"{num}/{count}" );

            await Task.Delay( 1000 );
        }
    }
}

上記のようなエディタ拡張は動作する

f:id:baba_s:20200325232219g:plain

非同期なので処理中も Unity エディタを操作できる