コガネブログ

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

【Unity】エディタ右下に非同期タスクの進捗を表示できる「Progress」を使いやすくするクラス

ソースコード

使用例

using Kogane;
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;

        using ( var scope = new ProgressScope( "ここにタスク名" ) )
        {
            for ( var i = 0; i < count; i++ )
            {
                scope.Report
                (
                    currentStep: i,
                    totalSteps: count,
                    description: $"{i + 1} / {count}"
                );

                yield return null;
            }
        }
    }
}

関連記事