kbengine_unity3d_warring
kbengine_unity3d_warring copied to clipboard
修复游戏中出现CompareBaseObjectsInternal的错误
游戏中出现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.的错误,经过分析是因为在某个非主线程地方使用了Unity API,即在某处非主线程对Unity内置类型的实例使用了!=,由于这个某处非常不好查找,Unity编辑器没有给出代码的具体定位,所以对所有Unity的内置类型实例的!=比较,统一替换为.Net自带函数的System.Object.ReferenceEquals来比较,经过测试,无论客户端采用多线程还是单线程运行,都不会再出现上述CompareBaseObjectsInternal错误。详细分析见:http://jiangyunfan2015.blog.163.com/blog/static/2483780502015410103446319.
游戏原来的错误,截图1。 该错误是由某个在非主线程的代码对Unity的内置类型实例使用了【!=】引起,但是这个某处很难找到, 截图中的红箭头表示类似的代码,不是指就是这处引发的,因为Unity编辑器没有给出具体提示。
截图1
把脚本中对所有Unity的内置类型实例的!=比较,统一替换为.Net自带函数的System.Object.ReferenceEquals来比较之后,游戏运行没有出现CompareBaseObjectsInternal的错误。见截图2.
截图2