[Unity Editor]Editor下使用协程

前段时间接到一个批处理FBX默认材质的任务,它需要在Unity回调中执行才能起作用。由于我的批处理逻辑和回调不是顺序执行,要得到正确的效果就需要等待一点时间,保证一个FBX处理完后才处理下一个。
  经过一番搜索找到了Editor下每帧执行的Update回调EditorApplication.update。Unity提供这个接口,是方便了我们,但是回调的形式有时却很繁琐……用的时候要+=,用完可能还要 -=。这里我想到了用Update来模拟协程,把繁琐的事情变成一次性的,就不能算繁琐~~
  下面上代码:

/// 
/// UnityEditor下模拟协程
/// 
public class EditorCoroutine
{
    /// 
    /// 迭代列表
    /// 
    private static List _itors = null;

    /// 
    /// 要从迭代列表中移除的索引
    /// 
    private static List _removeIdxs = null;

    private static EditorApplication.CallbackFunction _updateFunc = null;

    /// 
    /// Update方法是否在执行
    /// 
    private static bool _isUpdateRuning = false;

    static EditorCoroutine()
    {
        _itors = new List();
        _removeIdxs = new List();
        _updateFunc = _Update;
    }

    public static void Start(IEnumerator itor)
    {
        _itors.Add(itor);
        _RunUpdate(true);
    }

    private static void _RunUpdate(bool isRun)
    {
        if (isRun == _isUpdateRuning) return;

        // 运行Update
        if (isRun)
        {
            EditorApplication.update += _updateFunc;
        }
        // 停止运行Update
        else
        {
            EditorApplication.update -= _updateFunc;
        }
        _isUpdateRuning = isRun;
    }

    private static void _Update()
    {
        if (null == _itors || _itors.Count <= 0) return;
        for (int i = 0, c = _itors.Count; i < c; ++i)
        {
            if (!_itors[i].MoveNext())
            {
                _removeIdxs.Add(i);
            }
        }

        if (_removeIdxs.Count <= 0) return;
        for (int i = _removeIdxs.Count - 1; i >= 0; --i)
        {
            _itors.RemoveAt(_removeIdxs[i]);
            _removeIdxs.RemoveAt(i);
        }

        if (_itors.Count == 0)
        {
            _RunUpdate(false);
        }
    }
}

你可能感兴趣的:([Unity Editor]Editor下使用协程)