コガネブログ

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

【Unity】Unity 2020.1 新機能 - エディタ右下に非同期タスクの進捗を表示できる「Progress」クラスが追加された

概要

using System.Collections;
using Unity.EditorCoroutines.Editor;
using UnityEditor;
using UnityEngine;

public sealed class Example : EditorWindow
{
    [MenuItem( "Tools/Open" )]
    private static void Open()
    {
        GetWindow<Example>();
    }

    private void OnGUI()
    {
        if ( GUILayout.Button( "開始" ) )
        {
            EditorCoroutineUtility.StartCoroutine( OnUpdate(), this );
        }
    }

    private static IEnumerator OnUpdate()
    {
        const int count = 2000;

        var id = Progress.Start
        (
            name: "ここにタスク名",
            description: "ここに説明"
        );

        for ( var i = 0; i < count; i++ )
        {
            Progress.Report
            (
                id: id,
                currentStep: i,
                totalSteps: count,
                description: $"{i + 1} / {count}"
            );

            yield return null;
        }

        Progress.Remove( id );
    }
}

f:id:baba_s:20200824111945g:plain

f:id:baba_s:20200824111949g:plain

f:id:baba_s:20200824111956g:plain