Skip to content

Instantly share code, notes, and snippets.

@kankikuchi
Created September 1, 2020 22:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kankikuchi/b90327aa1186d49108f99fdd33e8489a to your computer and use it in GitHub Desktop.
Save kankikuchi/b90327aa1186d49108f99fdd33e8489a to your computer and use it in GitHub Desktop.
Unityのエディタ拡張で非同期タスクの進捗状況を表示する Progress【Unity】【エディタ拡張】
// EditorCoroutineSampleWindow.cs
// http://kan-kikuchi.hatenablog.com/entry/UnityEditor_Progress
//
// Created by kan.kikuchi on 2020.09.02.
using UnityEngine;
using System.Collections;//IEnumerator等を使うのに必要
using UnityEditor;//Progress等を使うのに必要
using Unity.EditorCoroutines.Editor;//EditorCoroutineUtilityを使うのに必要
/// <summary>
/// EditorCoroutineを試すためのサンプル
/// </summary>
public class EditorCoroutineSampleWindow : EditorWindow {
//タスクの名前と説明
private static string _taskName = "タスク名", _taskDescription = "タスクの説明";
//実行中のタスクのID
private static int _currentProgressID;
//実行中のコルーチン
private EditorCoroutine _currentCoroutine;
//=================================================================================
//初期化
//=================================================================================
//ウィンドウを開く
[MenuItem("Window/EditorCoroutineSample")]
private static void Open(){
GetWindow<EditorCoroutineSampleWindow>();
}
//=================================================================================
//更新
//=================================================================================
private void OnGUI() {
//タスク名とタスクの説明設定
_taskName = EditorGUILayout.TextField("タスク名", _taskName);
_taskDescription = EditorGUILayout.TextField("タスクの説明", _taskDescription);
//待機開始ボタン
if (GUILayout.Button("タスク開始")) {
//第2引数にobjectが必要(基本thisで大丈夫)
_currentCoroutine = EditorCoroutineUtility.StartCoroutine(UpdateProgress(), this);
}
//タスクの詳細を表示するボタン
if (GUILayout.Button("進捗の詳細表示")) {
Progress.ShowDetails();
}
//タスクが実行されていなければスルー
if (!Progress.Exists(_currentProgressID)) {
return;
}
//実行されているタスクのIDと状態を表示
Progress.Status taskStatus = Progress.GetStatus(_currentProgressID);
GUILayout.Label($"_progressID : {_currentProgressID} : {taskStatus}");
//タスクがキャンセル出来て、動いてる時にタスクをキャンセルボタンを表示
if (Progress.IsCancellable(_currentProgressID) && taskStatus == Progress.Status.Running && GUILayout.Button("タスクキャンセル")) {
//タスクキャンセル
Progress.Cancel(_currentProgressID);
//Progress.Finish(_currentProgressID, Progress.Status.Canceled);//これだとキャンセルは出来るがコールバックは実行されない
//コルーチンの停止
EditorCoroutineUtility.StopCoroutine(_currentCoroutine);
}
}
//進捗更新処理
private static IEnumerator UpdateProgress(){
//タスク開始、返り値でタスクIDの取得
_currentProgressID = Progress.Start(_taskName,_taskDescription);
//タスクキャンセル時のコールバック設定(設定しないとキャンセル出来ない)
Progress.RegisterCancelCallback(_currentProgressID, () => {
Debug.Log($"タスク : {_taskName}がキャンセルされました");
return true;
});
//タスク更新用のループ
var loopCount = 100;
for ( var i = 1; i <= loopCount; i++ ){
//進捗の更新
var progress = i / (float)loopCount;
Progress.Report(_currentProgressID, progress, $"{_taskDescription} ({i} / {loopCount})");
//0.1秒待機
yield return new EditorWaitForSeconds(0.1f);
}
//タスクの終了
Progress.Finish(_currentProgressID, Progress.Status.Succeeded);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment