dus*_*max 5 c# c++ java-native-interface android unity-game-engine
我正在尝试使用本机库来修改字节数组(实际上是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);
}
Run Code Online (Sandbox Code Playgroud)
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() {}
}
Run Code Online (Sandbox Code Playgroud)
上面的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;
}
Run Code Online (Sandbox Code Playgroud)
PostprocessNative似乎也成功调用了核心C ++函数(通过返回值验证),但是对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;
}
Run Code Online (Sandbox Code Playgroud)
我希望short []的所有值都为10,但是它们与调用JNI之前的值相同。
这是将短裤的Unity数组传递给C ++进行修改的正确方法吗?
首先,您没有提供有关您的配置的任何信息。您的脚本后端是什么:Mono还是IL2CPP?
其次,为什么不直接从C#调用C++代码?
1)转到:[文件] > [构建设置] > [播放器设置] > [播放器],然后打开[允许“不安全”代码]属性。
2)构建库后,将输出 .so 文件复制到Unity 项目中的Assets/Plugins/Android目录中。
C#代码:
using UnityEngine;
using UnityEngine.UI;
using System.Runtime.InteropServices;
using System;
public class CallNativeCode : MonoBehaviour
{
[DllImport("NativeCode")]
unsafe private static extern bool PostprocessNative(int width, int height, short* data_out);
public short[] dataShortOut;
public Text TxtOut;
public void Update()
{
dataShortOut = new short[100];
bool o = true;
unsafe
{
fixed (short* ptr = dataShortOut)
{
o = PostprocessNative(10, 10, ptr);
}
}
TxtOut.text = "Function out: " + o + " Array element 0: " + dataShortOut[0];
}
}
Run Code Online (Sandbox Code Playgroud)
C++代码:
#include <stdint.h>
#include <android/log.h>
#define LOG(...) __android_log_print(ANDROID_LOG_VERBOSE, "HamidYusifli", __VA_ARGS__)
extern "C"
{
bool PostprocessNative(int width, int height, short *data_out)
{
for (int y = 0; y < height; ++y)
{
for (int x = 0; x < width; ++x)
{
data_out[x + y * width] = 10;
}
}
LOG("Log: %d", data_out[0]);
// Changing the return value here is correctly reflected in C#.
return false;
}
}
Run Code Online (Sandbox Code Playgroud)