我在我的项目中大量使用了UniRx,但是当我第一次调用SchedulerOn(Scheduler.MainThread)时发现了一个问题,它不会在主thread.Uses代码中做这样的事情:
Login (new LoginReq (account.text, pwd.text, 1L)).SchedulerOn(Scheduler.MainThread).Done (x => {
Debug.Log("login req thread - " + System.Threading.Thread.CurrentThread.ManagedThreadId);
Package.Log("Login response - " + x.ToString());
if(x.errorMsg == null) {
Package.Log("login success - " + x.ToString());
//choice game world -> choice role
enterObj.SetActive(true);
loginObj.SetActive(false);
} else {
rspTips.text = x.ToString();
}
});输出显示,SetActive只能在主线程上调用。
这个问题特别是当我打开PC并第一次运行应用程序时。
是因为.NET热身吗?我不知道线程分配是如何工作的。
更新
我想我失去了一些重要的东西。
Login返回RSG.IPromise<T>(git主页在这里),用于轻量级的RSG.Promise任务,但是RSG.Promise lib没有调度程序module.So,我要用UniRx的IScheduler包装SchedulerPromise,代码没有新的技术。关于包装程序源代码,请看这个
我在这里呆了很长时间,time.Thanks有什么建议吗?
发布于 2017-03-07 14:34:28
UniRx在单独的线程上运行它的调度,但是统一是单线程的,而不是线程安全的。它防止访问其对象的代码在单独的线程上运行。
UniRX提供了一种将对可观察对象的订阅连接到其主线程上的方法,以允许运行访问Unity的代码:
.ObserveOnMainThread()
https://stackoverflow.com/questions/40847579
复制相似问题