2017-10-18 279 views
0

我正在开发Android插件,它应该从Java代码本身在OpenGL中加载纹理。我知道整个OpenGL上下文很棘手。这是Java代码:上面Unity中没有OpenGL上下文2017.1.2f1

 public static int Load(byte[] textureData) 
    { 
     final int[] textureHandle = new int[1]; 

     GLES30.glGenTextures(1, textureHandle, 0); 

      if (textureHandle[0] != 0) 
      { 
       final BitmapFactory.Options options = new BitmapFactory.Options(); 
       options.inScaled = false; // No pre-scaling 

       // Read in the resource 
      final Bitmap bitmap = BitmapFactory.decodeByteArray(textureData, 0, textureData.length, options); 

      // Bind to the texture in OpenGL 
      GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, textureHandle[0]); 

      // Set filtering 
      GLES30.glTexParameteri(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_MIN_FILTER, GLES30.GL_NEAREST); 
      GLES30.glTexParameteri(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_MAG_FILTER, GLES30.GL_NEAREST); 

      // Load the bitmap into the bound texture. 
      GLUtils.texImage2D(GLES30.GL_TEXTURE_2D, 0, bitmap, 0); 

      // Recycle the bitmap, since its data has been loaded into OpenGL. 
      bitmap.recycle(); 
     } 

     if (textureHandle[0] == 0) 
     { 
      throw new RuntimeException("Error loading texture."); 
     } 

     return textureHandle[0]; 
    } 

如果它来自例如按钮的onclick事件调用它会抛出一个异常:

调用的OpenGL ES API没有当前上下文(每个线程记录一次)

一些谷歌搜索后,我找到了解决这个问题的方法。当从调用我的Java代码时MonoBehaviour.OnRenderObject它工作得很好。正在加载纹理,每个人都很高兴。我在Unity作出这样的示例程序2017.1.2f1

然后,当我更新为2017.2.0f3它抛出

呼叫与没有当前上下文的OpenGL ES API(每线程记录一次)

再次,即使我没有做任何改变,Java代码仍然从MonoBehaviour.OnRenderObject调用。我试图更新到2017.3测试版,但它也引发异常...任何想法改变了什么?我现在被封锁了。我没有发现有关可能会在发行说明中引起更改的信息。

回答

0

我认为这是this issue的副本。

接受的答案表示您需要禁用多线程渲染。