游戏无法以快速启动,但没有例外

时间:2014-07-21 09:34:46

标签: unity3d shader

我正在尝试优化游戏以获得更好的性能。因此,我正在尝试启用脚本调用优化以使用快速但没有例外而不是慢速和安全。问题是,当在那种模式下,我收到了这个错误:

PropertyToID can only be called from the main thread.
我用Google搜索了一下,发现它可能是由某些着色器引起的。但我不知道如何找出哪一个或哪个。它应该是构造函数中的一些调用。

只有我在着色器上更改的内容才会在OnPostRender()函数中完成。我认为没关系。

1 个答案:

答案 0 :(得分:0)

确保您没有从任何构造函数或字段初始值设定项中调用Shader.PropertyToID。而不是将此代码移至Awake()Start()方法。像这样:

class MyClass: MonoBehaviour
{
    int somePropertyID = Shader.PropertyToID("SomePropertyID");   //<-- ERROR: this is field initializer

    MyClass()                                                     //<-- ERROR: this is constructor 
    {
        somePropertyID = Shader.PropertyToID("SomePropertyID");
    }

    void Awake()                                                   //<-- THIS IS OK!
    {
        somePropertyID = Shader.PropertyToID("SomePropertyID");
    }

    void Start()                                                   //<-- THIS IS OK!
    {
        somePropertyID = Shader.PropertyToID("SomePropertyID");
    }
}