Пример #1
0
        protected void CreateNativeTransformCollections()
        {
            _nativeTweenStates = new NativeArray <TweenTransformState>(_tweenStates.Length, Allocator.TempJob);
            JTweenTools.CopyTweenStateDirectlyToNativeArray(_tweenStates.buffer, _nativeTweenStates, _tweenStates.Length);

            _nativeTweenPositions = new NativeArray <TweenFloat3>(_tweenPositions.Length, Allocator.TempJob);
            JTweenTools.CopyTween3DirectlyToNativeArray(_tweenPositions.buffer, _nativeTweenPositions, _tweenPositions.Length);

            _nativeTweenRotations = new NativeArray <TweenRotation>(_tweenRotations.Length, Allocator.TempJob);
            JTweenTools.CopyTweenRotationDirectlyToNativeArray(_tweenRotations.buffer, _nativeTweenRotations, _tweenRotations.Length);

            _nativeTweenScales = new NativeArray <TweenFloat3>(_tweenScales.Length, Allocator.TempJob);
            JTweenTools.CopyTween3DirectlyToNativeArray(_tweenScales.buffer, _nativeTweenScales, _tweenScales.Length);

            _nativePositionLifetimes = new NativeArray <TweenLifetime>(_tweenPositionLifetimes.Length, Allocator.TempJob);
            JTweenTools.CopyTweenLifetimeDirectlyToNativeArray(_tweenPositionLifetimes.buffer, _nativePositionLifetimes, _tweenPositionLifetimes.Length);

            _nativeRotationLifetimes = new NativeArray <TweenLifetime>(_tweenRotationLifetimes.Length, Allocator.TempJob);
            JTweenTools.CopyTweenLifetimeDirectlyToNativeArray(_tweenRotationLifetimes.buffer, _nativeRotationLifetimes, _tweenRotationLifetimes.Length);

            _nativeScaleLifetimes = new NativeArray <TweenLifetime>(_tweenScaleLifetimes.Length, Allocator.TempJob);
            JTweenTools.CopyTweenLifetimeDirectlyToNativeArray(_tweenScaleLifetimes.buffer, _nativeScaleLifetimes, _tweenScaleLifetimes.Length);

            _nativePositions = new NativeArray <float3>(_tweenPositions.Length, Allocator.TempJob);
            _nativeRotations = new NativeArray <quaternion>(_tweenRotations.Length, Allocator.TempJob);
            _nativeScales    = new NativeArray <float3>(_tweenScales.Length, Allocator.TempJob);
        }
        protected override void UpdateTweens()
        {
            Profiler.BeginSample(UPDATE_PROFILE);
            if (_transforms.Length == 0)
            {
                Profiler.EndSample();
                return;
            }

            while (_tweenHandleActionEventQueue.Count > 0)
            {
                var updateLifetimes   = false;
                var tweenHandleAction = _tweenHandleActionEventQueue.Dequeue();
                if (tweenHandleAction.index >= 0 && tweenHandleAction.index < _tweenBatchHandles.Length)
                {
                    var tweenHandle = _tweenBatchHandles.buffer[tweenHandleAction.index];
                    var tweenBatch  = _tweenBatches.buffer[tweenHandleAction.index];
                    switch (tweenHandle.actionType)
                    {
                    case TweenHandleActionType.Play:
                        if (!tweenBatch.IsCompleted() && tweenBatch.IsPaused())
                        {
                            tweenBatch.state |= TweenStateType.IsPlaying;
                            tweenBatch.state &= ~TweenStateType.IsPaused;
                        }
                        break;

                    case TweenHandleActionType.Pause:
                        if (tweenBatch.IsPlaying())
                        {
                            tweenBatch.state &= ~TweenStateType.IsPlaying;
                            tweenBatch.state |= TweenStateType.IsPaused;
                        }
                        break;

                    case TweenHandleActionType.Stop:
                        if (tweenBatch.IsPlaying() || tweenBatch.IsPaused())
                        {
                            tweenBatch.state &= ~TweenStateType.IsPlaying;
                            tweenBatch.state &= ~TweenStateType.IsPaused;
                            tweenBatch.state |= TweenStateType.IsCompleted;
                            tweenBatch.state |= TweenStateType.JustEnded;
                        }
                        break;

                    case TweenHandleActionType.Recycle:
                        tweenBatch.state &= ~TweenStateType.IsPaused;
                        tweenBatch.state &= ~TweenStateType.IsPlaying;
                        tweenBatch.state &= ~TweenStateType.HasHandle;
                        tweenBatch.state |= TweenStateType.IsCompleted;
                        tweenBatch.state |= TweenStateType.RequiresRecycling;
                        break;

                    case TweenHandleActionType.Rewind:
                        tweenBatch.state = HANDLE_START_PAUSED;
                        var tweenBatchLifetimeRewind = _tweenBatchLifetimes.buffer[tweenHandleAction.index];
                        tweenBatchLifetimeRewind.Restart();
                        _tweenBatchLifetimes.buffer[tweenHandleAction.index] = tweenBatchLifetimeRewind;

                        updateLifetimes = true;
                        break;

                    case TweenHandleActionType.Restart:
                        tweenBatch.state = HANDLE_START_PLAYING;
                        var tweenBatchLifetimeRestart = _tweenBatchLifetimes.buffer[tweenHandleAction.index];
                        tweenBatchLifetimeRestart.Restart();
                        _tweenBatchLifetimes.buffer[tweenHandleAction.index] = tweenBatchLifetimeRestart;

                        updateLifetimes = true;

                        break;

                    case TweenHandleActionType.None:
                    default:
                        throw new NotImplementedException();
                    }

                    for (var i = tweenBatch.startIndex; i < tweenBatch.startIndex + tweenBatch.length; i++)
                    {
                        var tweenState = _tweenStates.buffer[i];
                        tweenState.state       = tweenBatch.state;
                        _tweenStates.buffer[i] = tweenState;

                        if (updateLifetimes)
                        {
                            var tweenPosLifetime = _tweenPositionLifetimes.buffer[i];
                            tweenPosLifetime.Restart();
                            _tweenPositionLifetimes.buffer[i] = tweenPosLifetime;

                            var tweenRotLifetime = _tweenRotationLifetimes.buffer[i];
                            tweenRotLifetime.Restart();
                            _tweenRotationLifetimes.buffer[i] = tweenRotLifetime;

                            var tweenScaleLifetime = _tweenScaleLifetimes.buffer[i];
                            tweenScaleLifetime.Restart();
                            _tweenScaleLifetimes.buffer[i] = tweenScaleLifetime;
                        }
                    }

                    tweenHandle.state = tweenBatch.state;

                    _tweenBatches.buffer[tweenHandleAction.index] = tweenBatch;
                }
                else
                {
                    Debug.LogWarning(RuntimeConstants.HANDLE_BATCH_NOT_FOUND);
                }
            }

            for (var i = 0; i < _tweenBatches.Length; i++)
            {
                var tweenBatch = _tweenBatches.buffer[i];
                if (tweenBatch.IsPlaying() && tweenBatch.JustStarted())
                {
                    tweenBatch.state &= ~TweenStateType.JustStarted;

                    _tweenBatches.buffer[i] = tweenBatch;

                    if (_tweenBatchHandles.buffer[i] != null)
                    {
                        _tweenBatchHandles.buffer[i].state = tweenBatch.state;
                        _tweenHandleCallbackEventQueue.Enqueue(_tweenBatchHandles.buffer[i]);
                    }
                }
            }

            // Create and setup native collections. Copy over existing data
            CreateNativeTransformCollections();

            _nativeTweenBatches = new NativeArray <TweenTransformBatchState>(_tweenBatches.Length, Allocator.TempJob);
            JTweenTools.CopyTweenBatchDirectlyToNativeArray(_tweenBatches.buffer, _nativeTweenBatches, _tweenBatches.Length);

            _nativeTweenBatchLifetimes = new NativeArray <TweenLifetime>(_tweenBatchLifetimes.Length, Allocator.TempJob);
            JTweenTools.CopyTweenLifetimeDirectlyToNativeArray(_tweenBatchLifetimes.buffer, _nativeTweenBatchLifetimes, _tweenBatchLifetimes.Length);

            // Schedule Jobs
            _processBatchJob = new ProcessBatchJob
            {
                deltaTime      = _deltaTime,
                batchLifetimes = _nativeTweenBatchLifetimes,
                tweenBatches   = _nativeTweenBatches
            };

            _processBatchJobHandle = _processBatchJob.Schedule(_tweenBatches.Length, 1);

            SetupJobs();

            _isJobScheduled = true;

            Profiler.EndSample();

            Profiler.BeginSample(EVENT_STARTED_PROFILE);

            // After all sensitive native work has completed, kick out any and all started events
            while (_tweenHandleCallbackEventQueue.Count > 0)
            {
                var tweenEvent = _tweenHandleCallbackEventQueue.Dequeue();
                tweenEvent.Started?.Invoke();
            }

            Profiler.EndSample();
        }