Passing byte array from Unity to Android (C++) for modification
Firstly, you did not provide any information about your configuration. What is your scripting backend: Mono or IL2CPP?
Secondly, why don't you call C++ code directly from C#?
1) Go to: [File] > [build Settings] > [Player Settings] > [Player] and turn on [Allow 'unsafe' Code] property.
2) After building the library, copy the output .so file(s) into the Assets/Plugins/Android directory in your Unity project.
C# code:
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++ code:
#include <stdint.h>
#include <android/log.h>
#define LOG(...) __android_log_print(ANDROID_LOG_VERBOSE, "0xBFE1A8", __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;
}
}