using System; using UnityEditor; using UnityEngine; using UnityEngine.Events; public abstract class EditorPerFrameActionBase { public event UnityAction onFinish; protected readonly string title; protected abstract int GetTotalCount(); protected abstract int GetCurrentIndex(); protected abstract void PerFrameAction(); protected virtual void OnFinish() { } protected EditorPerFrameActionBase() { title = GetType().ToString(); } public void Start() { EditorApplication.update = OnEditorUpdate; } private void OnEditorUpdate() { var finish = false; var error = false; try { var current = GetCurrentIndex(); var total = GetTotalCount(); if (current < total) { EditorUtility.DisplayProgressBar(title, string.Format("Action {0} / {1}", current, total), (float)current / total); PerFrameAction(); } else { finish = true; Debug.LogWarning("Editor Per Frame Action Complete " + GetType()); } } catch (Exception e) { finish = true; error = true; Debug.LogError(e.ToString()); Debug.LogError("Editor Per Frame Action Exit by Exception " + GetType()); } if (finish) { EditorApplication.update = null; EditorUtility.ClearProgressBar(); OnFinish(); if (onFinish != null) onFinish(!error, this); } } }