Beispiel #1
0
        public static AsyncDispatch Dispatch(IEnumerator task, bool showUI = false)
        {
            lock (dispatchQueue)
            {
                AsyncDispatch dispatch = new AsyncDispatch();

                dispatchQueue.Enqueue(() =>
                {
                    if (showUI)
                    {
                        EditorCoroutineRunner.StartCoroutineWithUI(DispatchCorotine(task, dispatch), "Dispatcher task", false);
                    }
                    else
                    {
                        EditorCoroutineRunner.StartCoroutine(task);
                    }
                });

                return(dispatch);
            }
        }
Beispiel #2
0
        public void Tick()
        {
            if (_coroutine != null)
            {
                if (_canceled)
                {
                    Stop();
                    return;
                }

                bool isWaiting = false;
                var  now       = DateTime.Now;
                if (_current != null)
                {
                    if (_currentType == typeof(WaitForSeconds))
                    {
                        var delta = now - _lastUpdateTime;
                        _timer -= (float)delta.TotalSeconds;

                        if (_timer > 0.0f)
                        {
                            isWaiting = true;
                        }
                    }
                    else if (_currentType == typeof(WaitForEndOfFrame) || _currentType == typeof(WaitForFixedUpdate))
                    {
                        isWaiting = false;
                    }
                    else if (_currentType == typeof(WWW))
                    {
                        var www = _current as WWW;
                        if (!www.isDone)
                        {
                            isWaiting = true;
                        }
                    }
                    else if (_currentType.IsSubclassOf(typeof(CustomYieldInstruction)))
                    {
                        var yieldInstruction = _current as CustomYieldInstruction;
                        if (yieldInstruction.keepWaiting)
                        {
                            isWaiting = true;
                        }
                    }
                    else if (_currentType == typeof(EditorCoroutine))
                    {
                        var editorCoroutine = _current as EditorCoroutine;
                        if (!editorCoroutine.HasFinished)
                        {
                            isWaiting = true;
                        }
                    }
                    else if (typeof(IEnumerator).IsAssignableFrom(_currentType))
                    {
                        if (_nestedCoroutine == null)
                        {
                            _nestedCoroutine = EditorCoroutineRunner.StartCoroutine(_current as IEnumerator);
                            isWaiting        = true;
                        }
                        else
                        {
                            isWaiting = !_nestedCoroutine.HasFinished;
                        }
                    }
                    else if (_currentType == typeof(Coroutine))
                    {
                        Debug.LogError("Nested Coroutines started by Unity's defaut StartCoroutine method are not supported in editor! please use EditorCoroutineRunner.Start instead. Canceling.");
                        _canceled = true;
                    }
                    else
                    {
                        Debug.LogError("Unsupported yield (" + _currentType + ") in editor coroutine!! Canceling.");
                        _canceled = true;
                    }
                }
                _lastUpdateTime = now;

                if (_canceled)
                {
                    Stop();
                    return;
                }

                if (!isWaiting)
                {
                    bool update = _coroutine.MoveNext();

                    if (update)
                    {
                        _current = _coroutine.Current;
                        if (_current != null)
                        {
                            _currentType = _current.GetType();

                            if (_currentType == typeof(WaitForSeconds))
                            {
                                var       wait      = _current as WaitForSeconds;
                                FieldInfo m_Seconds = typeof(WaitForSeconds).GetField("m_Seconds", BindingFlags.NonPublic | BindingFlags.Instance);
                                if (m_Seconds != null)
                                {
                                    _timer = (float)m_Seconds.GetValue(wait);
                                }
                            }
                            else if (_currentType == typeof(EditorStatusUpdate))
                            {
                                var updateInfo = _current as EditorStatusUpdate;
                                if (updateInfo.HasLabelUpdate)
                                {
                                    Label = updateInfo.Label;
                                }
                                if (updateInfo.HasPercentUpdate)
                                {
                                    PercentComplete = updateInfo.PercentComplete;
                                }
                            }
                        }
                    }
                    else
                    {
                        Stop();
                    }
                }
            }
        }