其中使用了该项目.NET的Async Socket代码。后来不知道什么时候这个奇怪的错误的出现:

CompareBaseObjectsInternal 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.

怪异是由于报错并没有影响程序运行,而是正确地接收了server返回的信息,而且通过回调正确显示出来。说明回调被运行了。可是从回调再调用一个解析器却一直不运行。是不是上面的错误导致的?

搜索百度CompareBaseObjectsInternal关键词仅仅有8个结果,http://blog.csdn.net/jixuguo/article/details/26693977相对有些意义,可是博文为转载。后来看到原文,大致的意思是假设从子线程调用主线程的callback。须要给回调加一个interface。

这个和我的情况不太一样,參考意义不大。

然后google。非常不幸古大哥一直载入载入。

。。

于是去yahoo,竟然还没有抽筋。

最前面搜索出来的几篇文章就是unity论坛上的。大致看出一些眉目。

http://forum.unity3d.com/threads/comparebaseobjectsinternal-error.184069/#post-1258700

http://forum.unity3d.com/threads/comparebaseobjectsinternal-can-only-be-called-from-the-main-thread-annoying.195902/http://forum.unity3d.com/threads/comparebaseobjectsinternal-can-only-be-called-from-the-main-thread-annoying.195902/

http://community.kii.com/t/error-comparebaseobjectsinternal-can-only-be-called-from-the-main-thread/378#!

http://answers.unity3d.com/questions/704284/comparebaseobjectsinternal-can-only-be-called-from-1.html

分析出来我的问题大概是在主线程里面注冊了回调,

//注冊全部事件的响应代码

        NetEventManager.GetInstance().netMessageReceived_event += OnNetMessageReceived;

        NetEventManager.GetInstance().gateServerConnected_event += OnGateServerConnected;

        NetEventManager.GetInstance().errorMessageOccured_event += OnErrorReceived;

然后socketclient对象是异步的,所以做了多线程处理,当socketclient接收到消息时,回调了主线程的函数(继承自monobehaviour),这时候就导致了报错。上面第一篇unity论坛博文解释得比較清楚,大概就是unity对于API调用主线程做了限制:

“Unity chose to limit API calls to main-thread, to make a simple and solid threading model that everyone can understand and use.”

依据这个解释,我把上面的代码凝视掉。程序就不报错了。

预计这些事件响应代码要放到非monobehavior的类里面去。就能够避免上面说的线程安全问题。

因为我感兴趣的多线程编程的基本或白色,上面的解释是正确的、有没有更好的解决办法。也希望你有种。

版权声明:本文博主原创文章。博客,未经同意不得转载。

Unity3d报告奇怪的错误CompareBaseObjectsInternal can only be called from the main thread.的更多相关文章

  1. Error: CompareBaseObjectsInternal can only be called from the main thread

    Posted: 01:39 PM 06-17-2013 hi, we're working on a project where we need to do some calculations on ...

  2. could not build module 'XXXXXXXX'或者error: expected identifier or '(' 。一堆奇怪的错误————错误根源

    一堆奇怪的错误:1⃣️could not build module 'XXXXXXXX' 2⃣️error: expected identifier or '(' 3⃣️EDIT Setting Pr ...

  3. Unity3d导入工程出现错误“Creating unique file”的解决方法

    Unity3d导入工程出现错误“Creating unique file:creating file Temp/tempFile failed.Please ensure there is enoug ...

  4. .net操作oracle,一定要用管理员身份运行 visual studio 啊,切记切记,免得报奇怪的错误。

    .net操作oracle,一定要用管理员身份运行 visual studio 啊,切记切记,免得报奇怪的错误.

  5. Error:(12, 64) java: 未报告的异常错误java.io.IOException; 必须对其进行捕获或声明以便抛出

    Error:(12, 64) java: 未报告的异常错误java.io.IOException; 必须对其进行捕获或声明以便抛出 package com.test; import org.apach ...

  6. 请教前辈:关于JS的一个奇怪的错误,不知是解析顺序造成的,还是什么原因。。

    各位前辈好,如题,不知道是HTML解析顺序造成的,还是JS预编译的结果(见注释). 烦请各位前辈进行指导. <!DOCTYPE html> <html> <head> ...

  7. celery 错误相关:Monkey-patching not on the main thread; threading.main_thread().join() will hang from a greenlet

    /Users/wangpingyang/.pyenv/versions/3.7.2/lib/python3.7/site-packages/httprunner/__init__.py:5: Monk ...

  8. [原]Unity3d中奇怪的编译错误

    整理项目,重新build时出现一些问题,这些代码在原项目中都是可以运行的. 错误信息如下: Assets/XXXXX.cs(79,35): error CS0103: The name `NNNNNN ...

  9. Java错误:很奇怪的错误。。。

    刚刚调试java web中出现了一个很奇怪的现象,前端有一个页面通过ajax调用后台的servlet,当我把后台的servlet代码修改后(将返回值由a修改为b),前端页面仍然获取的是a.调试跟踪se ...

随机推荐

  1. iOS开发UI篇--一个侧滑菜单SlidingMenu

    一.简介 侧滑菜单已经成为app一个极常用的设计,不管是事务类,效率类还是生活类app.侧滑菜单因Path 2.0和Facebook为开发者熟知,国内目前也有很多流行app用到了侧滑菜单,比如QQ.网 ...

  2. malloc和realloc

    malloc函数: extern void *malloc(unsigned int num_bytes); malloc 向系统申请分配指定size个字节的内存空间. 如果分配成功则返回指向被分配内 ...

  3. 前端实时消息提示的效果-websocket长轮询

    WebSocket是html5新增加的特性之一,可以实现客户端和服务器彼此之间相互通信,也可以实现跨域通信,目前大部分主流浏览器都支持,iE浏览器需要10版本以上. 需求:公司项目有一个报警模块,当后 ...

  4. Android中各种drawable的使用

    转载请说明出处.本文来自Android菜鸟:http://blog.csdn.net/android_cai_niao/article/details/46854767  QQ:2717521606 ...

  5. 18、IIC总线驱动程序

    i2c_s3c2410.c是内核自带dev层(adapt)驱动程序,知道怎么发收数据,不知道含义 在与i2c_s3c2410.c(在其probe函数中的s3c24xx_i2c_init函数会初始化ii ...

  6. [Angular] Show a loading indicator in Angular using *ngIf/else, the as keyword and the async pipe

    The network may be unreliable and loading data may take time. Thus it is important to give the user ...

  7. ios开发网络学习:一:NSURLConnection发送GET,POST请求

    #import "ViewController.h" @interface ViewController ()<NSURLConnectionDataDelegate> ...

  8. php javascript的ajax

    先说基础一点的get类型的ajax function loadXMLDoc() { var xmlhttp;//首先判断浏览器是否支持xmlhttprequest,因为ie56不是这个对象,是acti ...

  9. css3-2 CSS3选择器和文本字体样式

    css3-2   CSS3选择器和文本字体样式 一.总结 一句话总结:是要记下来的,记下来可以省很多事. 1.css的基本选择器中的:first-letter和:first-line是什么意思? :f ...

  10. 编辑器sublime、终端运行python

    sublime编辑器 Sublime Text 是一个代码编辑器(Sublime Text 2是收费软件,但可以无限期试用) Sublime Text是由程序员Jon Skinner于2008年1月份 ...