Beispiel #1
0
        private async Task <JAction> Do(bool onMainThread)
        {
            if (_executing == true && !_parallel)
            {
                Log.PrintError("JAction is currently executing, if you want to execute JAction multiple times at the same time, call Parallel() before calling Execute()");
                return(this);
            }
            if (!_parallel)
            {
                _executing = true;
            }
            _cancel = false;

            for (int i = 0; i < _toDo.Count; i++)
            {
                int index = i;

                if (_cancel || !Application.isPlaying)
                {
                    return(this);
                }

                //Delay
                if (_delays.ContainsKey(index))
                {
                    await Task.Delay((int)(_delays[index] * 1000));

                    continue;
                }

                //Wait Until
                if (_waits.ContainsKey(index))
                {
                    float _time = 0;
                    while (!_waits[index]())
                    {
                        if (_cancel)
                        {
                            return(this);
                        }

                        if (_timeout[index] > 0 && _time >= _timeout[index])
                        {
                            throw new TimeoutException();
                        }

                        await Task.Delay((int)(_frequency[index] * 1000));

                        _time += _frequency[index];
                    }
                    continue;
                }

                //Repeat When
                if (_whens.ContainsKey(index))
                {
                    float       _time      = 0;
                    Func <bool> _condition = _whenCauses[index];
                    float       _frequency = _whenFrequency[index];
                    float       _timeout   = _whenTimeout[index];
                    Action      _action    = _whens[index];
                    while (_condition())
                    {
                        if (_cancel)
                        {
                            return(this);
                        }

                        if (_timeout > 0 && _time >= _timeout)
                        {
                            throw new TimeoutException();
                        }

                        if (onMainThread)
                        {
                            await Task.Run(() =>
                            {
                                Loom.QueueOnMainThread(p =>
                                {
                                    _action();
                                }, null);
                            }, _cancellationTokenSource.Token);
                        }
                        else
                        {
                            await Task.Run(_action, _cancellationTokenSource.Token);
                        }


                        await Task.Delay((int)(_frequency * 1000));

                        _time += _frequency;
                    }
                    continue;
                }

                if (_toDo[index] != null)
                {
                    if (onMainThread)
                    {
                        await Task.Run(() =>
                        {
                            Loom.QueueOnMainThread(p =>
                            {
                                _toDo[index]();
                            }, null);
                        }, _cancellationTokenSource.Token);
                    }
                    else
                    {
                        await Task.Run(_toDo[index], _cancellationTokenSource.Token);
                    }
                }
            }
            _executing = false;
            return(this);
        }
Beispiel #2
0
 public JAction()
 {
     Reset();
     JActions.Add(this);
     Loom.Initialize();
 }
Beispiel #3
0
        private async Task <JAction> Do(bool onMainThread)
        {
            //这边不log的时候,会有概率跳过,迫不得已加了个Log
            if (onMainThread)
            {
                Log.Print($"正在往主线程增加JAction[{_index}]的任务");
            }

            if (_executing == true && !_parallel)
            {
                Log.PrintError("JAction is currently executing, if you want to execute JAction multiple times at the same time, call Parallel() before calling Execute()");
                return(this);
            }
            if (!_parallel)
            {
                _executing = true;
            }
            _cancel = false;

            for (int i = 0; i < _toDo.Count; i++)
            {
                int index = i;

                if (_cancel || !Application.isPlaying)
                {
                    break;
                }

                //Delay
                if (_delays.ContainsKey(index))
                {
                    await Task.Delay((int)(_delays[index] * 1000));

                    continue;
                }

                //DelayFrames
                if (_delayFrames.ContainsKey(index))
                {
                    //计算1帧时间(ms)
                    var durationPerFrame = 1000 / (int)(Application.targetFrameRate <= 0 ? GameStats.FPS : Application.targetFrameRate);
                    var duration         = durationPerFrame * _delayFrames[index];
                    await Task.Delay(duration);

                    continue;
                }


                //Wait Until
                if (_waits.ContainsKey(index))
                {
                    float _time = 0;
                    while (!_waits[index]())
                    {
                        if (_cancel)
                        {
                            return(this);
                        }

                        if (_timeout[index] > 0 && _time >= _timeout[index])
                        {
                            throw new TimeoutException();
                        }

                        await Task.Delay((int)(_frequency[index] * 1000));

                        _time += _frequency[index];
                    }
                    continue;
                }

                //Repeat When
                if (_whens.ContainsKey(index))
                {
                    float       _time      = 0;
                    Func <bool> _condition = _whenCauses[index];
                    float       _frequency = _whenFrequency[index];
                    float       _timeout   = _whenTimeout[index];
                    Action      _action    = _whens[index];
                    while (_condition())
                    {
                        if (_cancel)
                        {
                            return(this);
                        }

                        if (_timeout > 0 && _time >= _timeout)
                        {
                            throw new TimeoutException();
                        }

                        if (onMainThread)
                        {
                            await Task.Run(() =>
                            {
                                Loom.QueueOnMainThread(p =>
                                {
                                    _action();
                                }, null);
                            }, _cancellationTokenSource.Token);
                        }
                        else
                        {
                            await Task.Run(_action, _cancellationTokenSource.Token);
                        }


                        await Task.Delay((int)(_frequency * 1000));

                        _time += _frequency;
                    }
                    continue;
                }

                //Do
                await Task.Run(() =>
                {
                    void action(object p)
                    {
                        try
                        {
                            _toDo[index]?.Invoke();
                        }
                        catch (Exception e)
                        {
                            Log.PrintError($"JAction错误: {e.Message}, {e.Data["StackTrace"]},已跳过");
                        }
                    }
                    if (onMainThread)
                    {
                        Loom.QueueOnMainThread(action, null);
                    }
                    else
                    {
                        Loom.QueueOnOtherThread(action, null);
                    }
                }, _cancellationTokenSource.Token);
            }
            _executing = false;
            return(this);
        }
Beispiel #4
0
 void Awake()
 {
     _current    = this;
     initialized = true;
     UnityEngine.Object.DontDestroyOnLoad(this.gameObject);
 }
Beispiel #5
0
        private async Task <JAction> Do(bool onMainThread)
        {
            if (!_parallel)
            {
                _executing = true;
            }
            _cancel = false;

            for (int index = 0; index < _toDo.Count; index++)
            {
                if (_cancel || !Application.isPlaying)
                {
                    return(this);
                }

                //Delay
                if (_delays.ContainsKey(index))
                {
                    await Task.Delay((int)(_delays[index] * 1000));

                    continue;
                }

                //Wait Until
                if (_waits.ContainsKey(index))
                {
                    float _time = 0;
                    while (!_waits[index]())
                    {
                        if (_cancel)
                        {
                            return(this);
                        }

                        if (_timeout[index] > 0 && _time >= _timeout[index])
                        {
                            throw new TimeoutException();
                        }

                        await Task.Delay((int)(_frequency[index] * 1000));

                        _time += _frequency[index];
                    }
                    continue;
                }

                //Repeat When
                if (_whens.ContainsKey(index))
                {
                    float       _time      = 0;
                    Func <bool> _condition = _whenCauses[index];
                    float       _frequency = _whenFrequency[index];
                    float       _timeout   = _whenTimeout[index];
                    Action      _action    = _whens[index];
                    while (_condition())
                    {
                        if (_cancel)
                        {
                            return(this);
                        }

                        if (_timeout > 0 && _time >= _timeout)
                        {
                            throw new TimeoutException();
                        }

                        if (onMainThread)
                        {
                            await Task.Run(() =>
                            {
                                Loom.QueueOnMainThread(_action);
                            }, _cancellationTokenSource.Token);
                        }
                        else
                        {
                            await Task.Run(_action, _cancellationTokenSource.Token);
                        }


                        await Task.Delay((int)(_frequency * 1000));

                        _time += _frequency;
                    }
                    continue;
                }

                if (_toDo[index] != null)
                {
                    if (onMainThread)
                    {
                        await Task.Run(() =>
                        {
                            Loom.QueueOnMainThread(_toDo[index]);
                        }, _cancellationTokenSource.Token);
                    }
                    else
                    {
                        await Task.Run(_toDo[index], _cancellationTokenSource.Token);
                    }
                }
            }
            _executing = false;
            return(this);
        }
Beispiel #6
0
 void Awake()
 {
     _current    = this;
     initialized = true;
     DontDestroyOnLoad(gameObject);
 }