更新时间:2022-04-26 01:18:45
尝试一下: /*将其附加到场景中的任何对象上,以使其起作用*/
Try this: /* Attach this to any object in your scene, to make it work */
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class MainThread : MonoBehaviour {
class CallInfo
{
public Function func;
public object parameter;
public CallInfo(Function Func, object Parameter)
{
func = Func;
parameter = Parameter;
}
public void Execute()
{
func(parameter);
}
}
public delegate void Function(object parameter);
public delegate void Func();
static List<CallInfo> calls = new List<CallInfo>();
static List<Func> functions = new List<Func>();
static Object callsLock = new Object();
static Object functionsLock = new Object();
void Start()
{
calls = new List<CallInfo>();
functions = new List<Func>();
StartCoroutine(Executer());
}
public static void Call(Function Func, object Parameter)
{
lock(callsLock)
{
calls.Add(new CallInfo(Func, Parameter));
}
}
public static void Call(Func func)
{
lock(functionsLock)
{
functions.Add(func);
}
}
IEnumerator Executer()
{
while(true)
{
yield return new WaitForSeconds(0.01f);
while(calls.Count > 0)
{
calls[0].Execute();
lock(callsLock)
{
calls.RemoveAt(0);
}
}
while(functions.Count > 0)
{
functions[0]();
lock(functionsLock)
{
functions.RemoveAt(0);
}
}
}
}
}
像这样打电话
MainThread.Call(YourFunction);
MainThread.Call(YourFunction, parameters);
我认为您的问题是您正在从另一个不允许的线程中调用Unity方法
I think your problem is that you are calling Unity method from another thread which is not allowed