123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading;
- using UnityEngine;
- public struct DelayedQueueItem
- {
- public float time;
- public Action action;
- }
- public class Loom : MonoBehaviour
- {
- /// <summary>
- /// 最大线程数量
- /// </summary>
- public static int maxThreads = 8;
- /// <summary>
- /// 线程数量
- /// </summary>
- static int numThreads;
- private static Loom _currentLoom;
- public static Loom CurrentLoom
- {
- get
- {
- Initialize();
- return _currentLoom;
- }
- }
-
- /// <summary>
- /// 是否已经初始化
- /// </summary>
- static bool initialized;
- /// <summary>
- /// 作为初始化方法自己调用,可在初始化场景调用一次即可
- /// </summary>
- public static void Initialize()
- {
- if (!initialized)
- {
- if (!Application.isPlaying)
- return;
- initialized = true;
- GameObject g = new GameObject("Loom");
- //####永不销毁
- DontDestroyOnLoad(g);
- _currentLoom = g.AddComponent<Loom>();
- }
- }
- /// <summary>
- /// 延迟执行的队列
- /// </summary>
- private List<DelayedQueueItem> _delayedQueueItems = new List<DelayedQueueItem>();
-
- /// <summary>
- /// 目前正在延迟执行的队列
- /// </summary>
- private List<DelayedQueueItem> _currentDelayedQueueItems = new List<DelayedQueueItem>();
-
- /// <summary>
- /// 新添加的ACTION
- /// </summary>
- private List<Action> _newActions = new List<Action>();
- /// <summary>
- /// 不延迟执行
- /// </summary>
- private List<Action> _CurrentNoDelayActions = new List<Action>();
- public static void QueueOnMainThread(Action action)
- {
- QueueOnMainThread(action, 0f);
- }
- /// <summary>
- /// 添加到主线程执行
- /// </summary>
- /// <param name="action"></param>
- /// <param name="delayTime"></param>
- public static void QueueOnMainThread(Action action, float delayTime)
- {
- if (delayTime != 0)
- {
- if (CurrentLoom != null)
- {
- lock (CurrentLoom._delayedQueueItems)
- {
- CurrentLoom._delayedQueueItems.Add(new DelayedQueueItem { time = Time.time + delayTime, action = action });
- }
- }
- }
- else
- {
- if (CurrentLoom != null)
- {
- lock (CurrentLoom._newActions)
- {
- CurrentLoom._newActions.Add(action);
- }
- }
- }
- }
- public static Thread RunAsync(Action action)
- {
- Initialize();
- while (numThreads >= maxThreads)
- {
- //将当前线程挂起指定的毫秒数。
- Thread.Sleep(1);
- }
- //递增线程数量(Interlocked.Increment:以原子操作的形式递增指定变量的值并存储结果)
- Interlocked.Increment(ref numThreads);
- //将方法排入队列以便执行。 此方法在有线程池线程变得可用时执行
- ThreadPool.QueueUserWorkItem(RunAction, action);
- return null;
- }
- private static void RunAction(object action)
- {
- try
- {
- ((Action)action)();
- }
- catch
- {
- }
- finally
- {
- //递减线程数量(Interlocked.Increment:以原子操作的形式递增指定变量的值并存储结果)
- Interlocked.Decrement(ref numThreads);
- }
- }
- void Update()
- {
- lock (_newActions)
- {
- _CurrentNoDelayActions.Clear();
- _CurrentNoDelayActions.AddRange(_newActions);
- _newActions.Clear();
- }
- foreach (var actionItem in _CurrentNoDelayActions)
- {
- actionItem();
- }
- lock (_delayedQueueItems)
- {
- _currentDelayedQueueItems.Clear();
- _currentDelayedQueueItems.AddRange(_delayedQueueItems.Where(d => d.time <= Time.time));
-
- foreach (var item in _currentDelayedQueueItems)
- {
- _delayedQueueItems.Remove(item);
- }
- }
- foreach (var delayed in _currentDelayedQueueItems)
- {
- delayed.action();
- }
- }
- void OnDisable()
- {
- if (_currentLoom == this)
- {
- _currentLoom = null;
- }
- }
- }
|