123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- namespace NRKernal
- {
- using System;
- using System.Collections.Generic;
- using UnityEngine;
-
-
- public class AsyncTask<T>
- {
-
-
- private List<Action<T>> m_ActionsUponTaskCompletion;
-
-
-
-
- public AsyncTask(out Action<T> asyncOperationComplete)
- {
-
- if (!AsyncTask.IsInitialized)
- {
- AsyncTask.Init();
- }
- IsComplete = false;
- asyncOperationComplete = delegate (T result)
- {
- Result = result;
- IsComplete = true;
- if (m_ActionsUponTaskCompletion != null)
- {
- AsyncTask.PerformActionInUpdate(() =>
- {
- for (int i = 0; i < m_ActionsUponTaskCompletion.Count; i++)
- {
- m_ActionsUponTaskCompletion[i](result);
- }
- });
- }
- };
- }
-
-
- internal AsyncTask(T result)
- {
- Result = result;
- IsComplete = true;
- }
-
-
- public bool IsComplete { get; private set; }
-
-
- public T Result { get; private set; }
-
-
-
- public CustomYieldInstruction WaitForCompletion()
- {
- return new WaitForTaskCompletionYieldInstruction<T>(this);
- }
-
-
-
-
-
- public AsyncTask<T> ThenAction(Action<T> doAfterTaskComplete)
- {
-
- if (IsComplete)
- {
- doAfterTaskComplete(Result);
- return this;
- }
-
- if (m_ActionsUponTaskCompletion == null)
- {
- m_ActionsUponTaskCompletion = new List<Action<T>>();
- }
- m_ActionsUponTaskCompletion.Add(doAfterTaskComplete);
- return this;
- }
- }
-
- internal class AsyncTask
- {
-
- private static Queue<Action> m_UpdateActionQueue = new Queue<Action>();
-
- private static object m_LockObject = new object();
-
-
- public static bool IsInitialized { get; private set; }
-
-
-
-
- public static void PerformActionInUpdate(Action action)
- {
- lock (m_LockObject)
- {
- m_UpdateActionQueue.Enqueue(action);
- }
- }
-
- public static void OnUpdate()
- {
- if (m_UpdateActionQueue.Count == 0)
- {
- return;
- }
- lock (m_LockObject)
- {
- while (m_UpdateActionQueue.Count > 0)
- {
- Action action = m_UpdateActionQueue.Dequeue();
- action();
- }
- }
- }
-
- public static void Init()
- {
- if (IsInitialized)
- {
- return;
- }
- NRKernalUpdater.OnUpdate += OnUpdate;
- IsInitialized = true;
- }
- }
- }
|