如何在Android上使用unity CreateExternalTexture? [英] How to use unity CreateExternalTexture on Android?

查看:1076
本文介绍了如何在Android上使用unity CreateExternalTexture?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以帮我弄清楚我的代码有什么问题吗?我正在尝试从本机端加载图像并将纹理发送到Unity.我正在使用Unity Pro 5.0.2f1.

Can someone please help me figure out what is the problem with my code? I am trying to load an image from the native side and send the texture to Unity. I am using Unity Pro 5.0.2f1.

统一面:

void Start () {
AndroidJavaObject mImageLoader = new AndroidJavaObject("com.saeid.android.LoadTexture2D");
Texture2D texture2D = new Texture2D(1920, 1080, TextureFormat.ARGB32, false);

Int32 texPtr = mImageLoader.Call <Int32> ("loadImageReturnTexturePtr", "/storage/sdcard0/Images/test.jpg");
Debug.Log("texture pointer? " + texPtr);
Texture2D nativeTexture = Texture2D.CreateExternalTexture (1920, 1080, TextureFormat.ARGB32 , false, false, (IntPtr)texPtr);
texture2D.UpdateExternalTexture(nativeTexture.GetNativeTexturePtr());
gameObject.GetComponent<Renderer>().material.mainTexture = texture2D;
}

Java Side:

Java Side:

public int loadImageReturnTexturePtr(String imagePath) {
Log.d(LOGTAG, "loading image1: " + imagePath);

Bitmap bitmap = BitmapFactory.decodeFile(imagePath);
Log.d(LOGTAG, "Bitmap is: " + bitmap);

ByteBuffer buffer = ByteBuffer.allocate(bitmap.getByteCount());
bitmap.copyPixelsToBuffer(buffer);

int textures[] = new int[1];
GLES20.glGenTextures(1, textures, 0);
int textureId = textures[0];

GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureId);
GLES20.glTexImage2D(GLES20.GL_TEXTURE_2D, 0, GLES20.GL_RGBA, 1920, 1080, 0, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, buffer);
GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR);
GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S,
GLES20.GL_CLAMP_TO_EDGE);
GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T,
GLES20.GL_CLAMP_TO_EDGE);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, 0);
Log.d(LOGTAG, "texture id returned: " + textureId);

return textureId;
} 

推荐答案

所以,我知道了...代码实际上是正确的.除了两侧的纹理格式应相同. 就我而言,我有TextureFormat.ARGB32(在统一方面)和GLES20.GL_RGBA(在Java方面)不匹配.同样,GLES20.glTexImage2D(...)不适用于我.我用 GLUtils.texImage2D(GLES20.GL_TEXTURE_2D,0, bitmap,0);,最后我注意到相同的代码可在相同的Unity版本上使用,而在其他某些版本上则无法使用.例如,它不适用于5.0.2f1,但适用于5.0.3.

So, I figured it out... The code is actually correct. except the texture format in both side should be the same. In my case, I have TextureFormat.ARGB32 (in unity side) and GLES20.GL_RGBA (in Java side) which don't match. Also somehow GLES20.glTexImage2D(...) didn't work for me. I replaced it with GLUtils.texImage2D(GLES20.GL_TEXTURE_2D,0, bitmap,0); and finally I noticed the same code works on same Unity versions and doesn't work on some other. for example it is not working in 5.0.2f1 but it works in 5.0.3.

这篇关于如何在Android上使用unity CreateExternalTexture?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆