Skip to content

Instantly share code, notes, and snippets.

@mewlist
Last active December 8, 2019 15:32
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 mewlist/04b40a629d27ebe7d29b68ca4b6f6e95 to your computer and use it in GitHub Desktop.
Save mewlist/04b40a629d27ebe7d29b68ca4b6f6e95 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using UnityEditor;
using UnityEngine;
namespace Mewlist
{
public class EditorBatchProcessor
{
private List<Func<Task>> tasks = new List<Func<Task>>();
private bool frameProgression = false;
public async Task Run()
{
EditorApplication.update += OnUpdate;
try
{
await Process();
}
catch (Exception e)
{
Debug.Log(e);
}
EditorApplication.update -= OnUpdate;
}
public void Add(Func<Task> action) => tasks.Add(action);
public void SelectObject(GameObject go) => Add(async () => Selection.activeObject = go);
private async Task Process()
{
foreach (var action in tasks)
{
Debug.Log("[EditorBatchProcessor] " + action.Method.Name + "()");
await action();
await WaitForNextFrame();
}
}
private void OnUpdate() => frameProgression = true;
private async Task WaitForNextFrame()
{
frameProgression = false;
while (!frameProgression) await Task.Delay(100);
}
}
}
// 使い方
async Task ProcessAsset()
{
var processor = new EditorBatchProcessor();
// 何らかのゲームオブジェクトへのコンポーネント登録操作
var go = new GameObject();
var component = go.AddComponent<HogeComponent>();
async Task FirstStep()
{
// 任意のEditor拡張処理
}
async Task LastStep()
{
// インスペクタ表示後の処理
}
// 処理を手続き的に登録
processor.Add(FirstStep)
processor.SelectObject(go); // インスペクタを一度表示する
processor.Add(LastStep)
await processor.Run();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment