using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//跨线程访问类,继承自单例类
public class AccrossThreadHelper : MonoSingleton<AccrossThreadHelper>
{
//定义个无参数无返回值的委托类型(可根据需求改进)
public delegate void AccrossThreadFunc();
//一个委托集合
private List<AccrossThreadFunc> delegateList;
private void Start()
{
//初始化委托集合,并开启协程(Update替代协程也可以)
delegateList = new List<AccrossThreadFunc>();
StartCoroutine(Done());
}
//协程具体实现
IEnumerator Done()
{
while(true)
{
if (delegateList.Count > 0)
{
//遍历委托链,依次执行
foreach (AccrossThreadFunc item in delegateList)
{
item();
}
//执行过的委托代码,清空
delegateList.Clear();
}
yield return new WaitForSeconds(0.1f);
}
}
//公开的添加委托方法
public void AddDelegate(AccrossThreadFunc func)
{
delegateList.Add(func);
}
}
Unity3D跨线程访问类
最新推荐文章于 2023-09-21 09:56:05 发布