Ejemplo n.º 1
0
        /// <summary>
        /// Process all queued events.
        /// </summary>
        public void ProcessEvents()
        {
            // Early bail to avoid setting up job stuff unnecessarily
            if (_queuedEvents.Length == 0)
            {
                return;
            }

            NativeQueue <UnityEvent <T_Event> > eventsToProcessQueue =
                new NativeQueue <UnityEvent <T_Event> >(Allocator.TempJob);

            BuildEventQueueJob job = new BuildEventQueueJob();

            job.queuedEvents    = _queuedEvents;
            job.subscribers     = _subscribers;
            job.eventsToProcess = eventsToProcessQueue.ToConcurrent();

            job.Schedule(_queuedEvents.Length, _batchCount).Complete();

            while (eventsToProcessQueue.TryDequeue(out UnityEvent <T_Event> ev))
            {
                try
                {
                    _subscriberCallbacks[ev.subscriberIndex](ev.ev);
                }
                catch (Exception e)
                {
                    Debug.LogException(e);
                }
            }

            eventsToProcessQueue.Dispose();
            _queuedEvents.Clear();
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Process all queued events.
        /// </summary>
        public void ProcessEvents()
        {
            // Early bail to avoid setting up job stuff unnecessarily
            if (_queuedEvents.Length == 0)
            {
                return;
            }

            NativeQueue <UnityEventJob> eventsToProcessQueue = new NativeQueue <UnityEventJob>(Allocator.TempJob);

            BuildEventQueueJob job = new BuildEventQueueJob();

            job.queuedEvents    = _queuedEvents;
            job.subscribers     = _subscribers;
            job.eventsToProcess = eventsToProcessQueue.AsParallelWriter();

            job.Schedule(_queuedEvents.Length, 32).Complete();

            int eventCount = eventsToProcessQueue.Count;

            NativeArray <UnityEventJob> eventsToProcess = new NativeArray <UnityEventJob>(eventCount, Allocator.TempJob);

            EventQueueToEventArrayJob setJob = new EventQueueToEventArrayJob();

            setJob.eventsInQueue = eventsToProcessQueue;
            setJob.events        = eventsToProcess;

            JobHandle setArrayHandle = setJob.Schedule();

            NativeArray <T_Job> jobsArray = new NativeArray <T_Job>(eventCount, Allocator.TempJob);

            CreateJobsArrayJob createJobArrayJob = new CreateJobsArrayJob();

            createJobArrayJob.events      = eventsToProcess;
            createJobArrayJob.subscribers = _subscribers;
            createJobArrayJob.jobs        = jobsArray;

            JobHandle createJobArrayHandle = createJobArrayJob.Schedule(
                eventCount,
                _batchCount,
                setArrayHandle);

            ExecuteEventJobsJob executeJob = new ExecuteEventJobsJob();

            executeJob.jobsResult = jobsArray;
            executeJob.evs        = eventsToProcess;

            JobHandle executeHandle = executeJob.Schedule(
                eventCount,
                _batchCount,
                createJobArrayHandle);

            WriteBackToSubscribersJob writeBackJob = new WriteBackToSubscribersJob();

            writeBackJob.subscribers = _subscribers;
            writeBackJob.evs         = eventsToProcess;
            writeBackJob.jobsResult  = jobsArray;

            JobHandle writeBackHandle = writeBackJob.Schedule(
                eventCount,
                _batchCount,
                executeHandle);

            writeBackHandle.Complete();

            int count = eventsToProcess.Length;

            for (int i = 0; i < count; i++)
            {
                UnityEventJob ev  = eventsToProcess[i];
                Subscription  sub = _subscribers[ev.subscriberIndex];

                try
                {
                    _subscriberCallbacks[ev.subscriberIndex](sub.job);
                }
                catch (Exception e)
                {
                    Debug.LogException(e);
                }
            }

            eventsToProcessQueue.Dispose();
            eventsToProcess.Dispose();
            jobsArray.Dispose();

            _queuedEvents.Clear();

#if !DISABLE_EVENT_SAFETY_CHKS
            _cachedCurEvents.Clear();
#endif
        }