/// <summary> /// Add a new callback function to the worker thread. /// The 'main' delegate will run on a secondary thread, while the 'finished' delegate will run in Update(). /// Return 'false' if you want the same delegates to execute again next time, or 'true' if you're done. /// </summary> static public void CreateMultiStage(BoolFunc main, BoolFunc finished = null, bool highPriority = false) { #if SINGLE_THREADED if (main != null) { while (!main()) { } } if (finished != null) { while (!finished()) { } } #else if (mInstance == null) { #if UNITY_EDITOR if (!Application.isPlaying) { if (main != null) { while (!main()) { } } if (finished != null) { while (!finished()) { } } return; } #endif GameObject go = new GameObject("Worker Thread"); mInstance = go.AddComponent <WorkerThread>(); } Entry ent; if (mInstance.mUnused.size != 0) { lock (mInstance.mUnused) { ent = (mInstance.mUnused.size != 0) ? mInstance.mUnused.Pop() : new Entry(); } } else { ent = new Entry(); } ent.mainBool = main; ent.finishedBool = finished; ent.milliseconds = 0; if (highPriority) { lock (mInstance.mPriority) mInstance.mPriority.Enqueue(ent); } else { lock (mInstance.mRegular) mInstance.mRegular.Enqueue(ent); } #endif }
/// <summary> /// Add a new callback function to the worker thread. /// </summary> static public void Create(VoidFunc main, EnumFunc finished, bool highPriority = false) { #if SINGLE_THREADED if (main != null) { main(); } while (finished().MoveNext()) { } #else if (mInstance == null) { #if UNITY_EDITOR if (!Application.isPlaying) { if (main != null) { main(); } if (finished != null) { finished(); } return; } #endif var go = new GameObject("Worker Thread"); mInstance = go.AddComponent <WorkerThread>(); } Entry ent; if (mInstance.mUnused.size != 0) { lock (mInstance.mUnused) { ent = (mInstance.mUnused.size != 0) ? mInstance.mUnused.Pop() : new Entry(); } } else { ent = new Entry(); } ent.main = main; ent.finishedEnum = finished; ent.milliseconds = 0; if (main != null) { if (highPriority) { lock (mInstance.mPriority) mInstance.mPriority.Enqueue(ent); } else { lock (mInstance.mRegular) mInstance.mRegular.Enqueue(ent); } } else { lock (mInstance.mFinished) mInstance.mFinished.Enqueue(ent); } #endif }