2017-10-18 286 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的副本。

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