c# - 将字节数组从 Unity 传递到 Android (C++) 进行修改

标签 c# android c++ unity-game-engine java-native-interface

我正在尝试使用 native 库来修改字节数组(实际上是 uint16 数组)的内容。我有 Unity (C#) 中的数组和 C++ 中的 native 库。

我已经尝试了一些方法,我能做到的最好的办法就是成功调用 native 代码并能够将 bool 值返回给 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的所有修改都不会反射(reflect)在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 的 Shorts 数组传递到 C++ 进行修改的正确方法吗?

最佳答案

首先,您没有提供有关您的配置的任何信息。您的脚本后端是什么:Mono 还是 IL2CPP

其次,为什么不直接从 C# 调用 C++ 代码?

1) 转到:[文件] > [build设置] > [播放器设置] > [播放器] 并打开[允许“不安全”代码]属性。

2) 构建库后,将输出 .so 文件复制到 Unity 项目中的 Assets/Plugins/Android 目录中。

enter image description here

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];
    }
}

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;
    }
}

关于c# - 将字节数组从 Unity 传递到 Android (C++) 进行修改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58447053/

相关文章:

C++ 管道和 fork

c# - 单元测试 ReSharper 和 NUnit 给出不同的结果

C#正则表达式匹配字符串末尾的数字

c# - 如何获取光标所在函数中方法的方法体

android - 警报管理器不工作

android - 如何隐藏自定义选项卡操作栏

Android:为什么当我膨胀资源布局文件时我的用户界面没有被绘制?

c++ - 我有两个网络摄像头,但 QCameraInfo::availableCameras 返回空数组

c# - 如何将 Ninject 命名绑定(bind)与 DependencyResolver 和 PropertyInjection 一起使用

C++:以下几种情况会不会内存泄漏?