我正在尝试使用本机库来修改字节数组(实际上是uint16数组)的内容。我在Unity(C#)中拥有数组,在C ++中拥有本地库。
我已经尝试了几件事,我能做的最好的事情就是成功调用本机代码并能够将布尔值返回给C#。当我在C ++中传递数组并将其变异时,问题就来了。无论我做什么,该数组在C#中都保持不变。
这是我在Unity方面拥有的:
// In Update().
using (AndroidJavaClass processingClass = new AndroidJavaClass(
"com.postprocessing.PostprocessingJniHelper"))
{
if (postprocessingClass == null) {
Debug.LogError("Could not find the postprocessing class.");
return;
}
short[] dataShortIn = ...; // My original data.
short[] dataShortOut = new short[dataShortIn.Length];
Buffer.BlockCopy(dataShortIn, 0, dataShortOut, 0, dataShortIn.Length);
bool success = postprocessingClass.CallStatic<bool>(
"postprocess", TextureSize.x, TextureSize.y,
dataShortIn, dataShortOut);
Debug.Log("Processed successfully: " + success);
}
Unity项目在Plugins / Android中具有postprocessing.aar,并已为Android构建平台启用。我在Java中有一个JNI层(已成功调用):
public final class PostprocessingJniHelper {
// Load JNI methods
static {
System.loadLibrary("postprocessing_jni");
}
public static native boolean postprocess(
int width, int height, short[] inData, short[] outData);
private PostprocessingJniHelper() {}
}
上面的Java代码在C ++中调用此代码。
extern "C" {
JNIEXPORT jboolean JNICALL POSTPROCESSING_JNI_METHOD_HELPER(postprocess)(
JNIEnv *env, jclass thiz, jint width, jint height, jshortArray inData, jshortArray outData) {
jshort *inPtr = env->GetShortArrayElements(inData, nullptr);
jshort *outPtr = env->GetShortArrayElements(outData, nullptr);
jboolean status = false;
if (inPtr != nullptr && outPtr != nullptr) {
status = PostprocessNative(
reinterpret_cast<const uint16_t *>(inPtr), width, height,
reinterpret_cast<uint16_t *>(outPtr));
}
env->ReleaseShortArrayElements(inData, inPtr, JNI_ABORT);
env->ReleaseShortArrayElements(outData, outPtr, 0);
return status;
}
核心C ++函数PostprocessNative
似乎也已成功调用(通过返回值验证,但是对data_out的所有修改都不会在Unity中反映出来。
bool PostprocessNative(const uint16_t* data_in, int width,
int height, uint16_t* data_out) {
for (int y = 0; y < height; ++y) {
for (int x = 0; x < width; ++x) {
data_out[x + y * width] = 10;
}
}
// Changing the return value here is correctly reflected in C#.
return false;
}
我希望short []的所有值都为10,但它们等于调用JNI之前的值。
这是将短裤的Unity数组传递给C ++进行修改的正确方法吗?
GetShortArrayElements
可以将Java数组固定在内存中,或者返回数据的副本。因此,使用完指针后,应该调用ReleaseShortArrayElements
。
env->ReleaseShortArrayElements(inData, inPtr, JNI_ABORT); // free the buffer without copying back the possible changes
env->ReleaseShortArrayElements(outData, outPtr, 0); // copy back the content and free the buffer