Unity脚本在.NET3.5到.NET4.0之间无声地失败

Unity脚本在.NET3.5到.NET4.0之间无声地失败,.net,firebase,unity3d,callback,.net,Firebase,Unity3d,Callback,今天,我尝试将我的项目脚本运行时版本从使用.NET3.5等效版本更改为.NET4.x等效版本。从那时起,该项目一直存在各种各样的问题。脚本随机失败。它们将执行某些功能或其他功能,但随后会在看似随机的点停止。它们失败的点在两次运行之间保持一致,但是如果我编辑脚本,它们可能会改变。关于这些失败,最奇怪的是控制台中没有生成错误。有些错误会被记录,但只有在以后运行的某些代码依赖于前一个函数突然结束时未执行的操作时,才会发生这种情况。我不知道是什么导致了这样的事情,我甚至还没能想出一个谷歌搜索,这是远程有

今天,我尝试将我的项目脚本运行时版本从使用.NET3.5等效版本更改为.NET4.x等效版本。从那时起,该项目一直存在各种各样的问题。脚本随机失败。它们将执行某些功能或其他功能,但随后会在看似随机的点停止。它们失败的点在两次运行之间保持一致,但是如果我编辑脚本,它们可能会改变。关于这些失败,最奇怪的是控制台中没有生成错误。有些错误会被记录,但只有在以后运行的某些代码依赖于前一个函数突然结束时未执行的操作时,才会发生这种情况。我不知道是什么导致了这样的事情,我甚至还没能想出一个谷歌搜索,这是远程有用的。恢复到3.5会使问题消失,但鉴于3.5已经贬值,我不知道我的插件还能支持多久

我认为这可能是因为我在项目中使用的SDK、Firebase数据库和Firebase Auth造成的。

在问题区域放置try/catch会在Unity中产生异常。错误如下:

UnityException: EXAMPLE can only be called from the main thread.
Constructors and field initializers will be executed from the loading thread when loading a scene.
Don't use this function in the constructor or field initializers, instead move initialization code to the Awake or Start function.
每次脚本失败时,函数调用都源自.ContinueWith()语句。失败发生在我第一次尝试访问这些线程中的Unity对象时

Unity对象从未能够在Unity的主线程之外访问。使用.net 3.5等效脚本运行时版本的ContinueWith()任务作为此主线程的一部分进行处理。使用.NET4.x等效程序,它们作为单独的线程处理。令人沮丧的是,Unity在默认情况下没有捕捉到这个错误

将System.Threading.Tasks.TaskScheduler添加到.ContinueWith()可以将其调度回主线程。在主线程中调用和存储System.Threading.Tasks.TaskScheduler.FromCurrentSynchronizationContext()将为任务计划程序提供对主线程的引用

private void Awake() {
    System.Threading.Tasks.TaskScheduler taskScheduler = System.Threading.Tasks.TaskScheduler.FromCurrentSynchronizationContext();
    TaskCompletionSource<T> tcs = new TaskCompletionSource<T>();
    tcs.Task.ContinueWith(task => {
        // Your code here
    }, taskScheduler);
}
private void Awake(){
System.Threading.Tasks.TaskScheduler TaskScheduler=System.Threading.Tasks.TaskScheduler.FromCurrentSynchronizationContext();
TaskCompletionSource tcs=新的TaskCompletionSource();
tcs.Task.ContinueWith(任务=>{
//你的代码在这里
},任务调度器);
}
起初我认为这个错误与Firebase有关。这是一个错误的假设,恰好我的Firebase函数包含了大多数.ContinueWith()函数调用。

在问题区域放置try/catch会在Unity中生成异常。错误如下:

UnityException: EXAMPLE can only be called from the main thread.
Constructors and field initializers will be executed from the loading thread when loading a scene.
Don't use this function in the constructor or field initializers, instead move initialization code to the Awake or Start function.
每次脚本失败时,函数调用都源自.ContinueWith()语句。失败发生在我第一次尝试访问这些线程中的Unity对象时

Unity对象从未能够在Unity的主线程之外访问。使用.net 3.5等效脚本运行时版本的ContinueWith()任务作为此主线程的一部分进行处理。使用.NET4.x等效程序,它们作为单独的线程处理。令人沮丧的是,Unity在默认情况下没有捕捉到这个错误

将System.Threading.Tasks.TaskScheduler添加到.ContinueWith()可以将其调度回主线程。在主线程中调用和存储System.Threading.Tasks.TaskScheduler.FromCurrentSynchronizationContext()将为任务计划程序提供对主线程的引用

private void Awake() {
    System.Threading.Tasks.TaskScheduler taskScheduler = System.Threading.Tasks.TaskScheduler.FromCurrentSynchronizationContext();
    TaskCompletionSource<T> tcs = new TaskCompletionSource<T>();
    tcs.Task.ContinueWith(task => {
        // Your code here
    }, taskScheduler);
}
private void Awake(){
System.Threading.Tasks.TaskScheduler TaskScheduler=System.Threading.Tasks.TaskScheduler.FromCurrentSynchronizationContext();
TaskCompletionSource tcs=新的TaskCompletionSource();
tcs.Task.ContinueWith(任务=>{
//你的代码在这里
},任务调度器);
}

起初我认为这个错误与Firebase有关。这是一个错误的假设,恰好我的Firebase函数包含了我的大部分.ContinueWith()函数调用。

任务在引入async/await关键字之前就存在了,因此提供了不依赖于语言执行continuations的方法。尽管这些方法仍然可以使用,但我们通常建议您更喜欢
async/await
而不是使用
ContinueWith
ContinueWith
也不会捕获
SynchronizationContext
,因此实际上在语义上与
async/await
不同

而不是使用

private void Awake(){
System.Threading.Tasks.TaskScheduler TaskScheduler=System.Threading.Tasks.TaskScheduler.FromCurrentSynchronizationContext();
TaskCompletionSource tcs=新的TaskCompletionSource();
tcs.Task.ContinueWith(任务=>{
//你的代码在这里
},任务调度器);
}
使用以下命令:

private异步任务DosomethingAsync()
{
var result=await CallDependencyAsync();
返回结果+1;
}
我想说的是,与其说是继续,不如说是等待。
无论如何,

任务在引入async/await关键字之前就已经存在,因此提供了不依赖于语言执行continuations的方法。尽管这些方法仍然可以使用,但我们通常建议您更喜欢
async/await
而不是使用
ContinueWith
ContinueWith
也不会捕获
SynchronizationContext
,因此实际上在语义上与
async/await
不同

而不是使用

private void Awake(){
System.Threading.Tasks.TaskScheduler TaskScheduler=System.Threading.Tasks.TaskScheduler.FromCurrentSynchronizationContext();
TaskCompletionSource tcs=新的TaskCompletionSource();
tcs.Task.ContinueWith(任务=>{
//你的代码在这里
},任务调度器);
}
使用以下命令:

private异步任务DosomethingAsync()
{
var result=await CallDependencyAsync();
返回结果+1;
}
什么