public static Thread RunAsync(Action begin, Action end) { if (!GameCommon.IsMainThread()) { if (begin != null) { begin(); } if (end != null) { end(); } return(null); } Initialize(); while (numThreads >= maxThreads) { Thread.Sleep(1); } Interlocked.Increment(ref numThreads); TaskItem item; { item = GenTaskItem(); item.Init(begin, end); } ThreadPool.QueueUserWorkItem(RunAction, item); return(null); }
private static void Initialize() { if (!initialized) { if (GameCommon.IsMainThread()) { if (!Application.isPlaying) { return; } initialized = true; var g = new GameObject("ThreadTask"); UnityEngine.Object.DontDestroyOnLoad(g); _current = g.AddComponent <ThreadTask>(); } } }