Unity Loom (原始)

来自插件:QR Code/Barcode Scanner and Generator--Cross Platform(Pro)

https://assetstore.unity.com/packages/tools/integration/qr-code-barcode-scanner-and-generator-cross-platform-pro-56083

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;
using System.Threading;
using System.Linq;
			
//Loom handles threading
public class Loom : MonoBehaviour
{
	private static Loom _current;
		
	public static Loom Current
	{
		get
		{
			if (_current == null && Application.isPlaying)
			{
			    
				var g = GameObject.Find("Loom");
				if(g==null)
				{
					g = new GameObject("Loom");
				}
				
				_current = g.GetComponent<Loom>() ?? g.AddComponent<Loom>();
			}
						
			return _current;
		}
	}
	
	void Awake()
	{
		if(_current != null && _current != this)
		{
			Destroy(gameObject);
		}
		else
		{
			_current = this;
		}
	}
		
	private List<Action> _actions = new List<Action>();
	public class DelayedQueueItem
	{
		public float time;
		public Action action;
		public string name;
	}
	private List<DelayedQueueItem> _delayed = new  List<DelayedQueueItem>();
				
	public static void QueueOnMainThread(Action action, float time, string name)
	{
		lock(Current._delayed)
		{
			if(Current._delayed.Any(d=>d.name==name))
				return;
			QueueOnMainThread(action, time);
		}
	}
	
	public static void QueueOnMainThread(Action action, string name)
	{
		QueueOnMainThread(action, 0, name);
	}
	
	public static void QueueOnMainThread(Action action, float time)
	{
		if (time != 0)
		{
			lock (Current._delayed)
			{
				Current._delayed.Add(new DelayedQueueItem { time = Time.time + time, action = action});
			}
		}
		else
		{
			lock (Current._actions)
			{
				Current._actions.Add(action);
			}
		}
	}
	
	public static void QueueOnMainThread(Action action)
	{
		lock (Current._actions)
		{
			Current._actions.Add(action);
		}
	}
				
	public static void RunAsync(Action a)
	{
		var t = new Thread(RunAction);
		t.Priority = System.Threading.ThreadPriority.Normal;
		t.Start(a);
	}
				
	private static void RunAction(object action)
	{
		((Action)action)();
	}
	
				
	List<Action> toBeRun = new List<Action>();
	List<DelayedQueueItem> toBeDelayed = new List<DelayedQueueItem>();
	
	void Update()
	{
		//Process the non-delayed actions
		lock (_actions)
		{
			toBeRun.AddRange(_actions);
			_actions.Clear();
		}
		foreach (var a in toBeRun)
		{
			try			{
				a();
			}
			catch (Exception e)
			{
				Debug.LogError("Queued Exception: " + e.ToString());
			}
		}
		toBeRun.Clear();
		lock (_delayed)
		{
			toBeDelayed.AddRange(_delayed);
		}
		foreach (var delayed in toBeDelayed.Where(d=>d.time <= Time.time))
		{
			lock (_delayed)
			{
				_delayed.Remove(delayed);
			}
			try
			{
				delayed.action();
			}
			catch (Exception e)
			{
				Debug.LogError("Delayed Exception:" + e.ToString());
			}
		}
		toBeDelayed.Clear();				
	}
}
		

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值