I have some JavaScript data which I am serializing so that Unity can consume it as a byte[] array, as generally described here. I want to expose this data to Unity via a jslib interface like this:
[DllImport("__Internal")]
private static extern byte[] GetByteArray();
I attempted to bridge my values from JavaScript using the built-in methods in emscripten:
GetByteArray: function () {
var myTypedArray = new Uint8Array([10, 20, 30, 40, 50]);
var buf = _malloc(myTypedArray.length * myTypedArray.BYTES_PER_ELEMENT);
writeArrayToMemory(myTypedArray, buf);
return buf;
},
Unfortunately, this does not work -- the webassembly C# just sees a 0-length array as the return type. I also tried stuff like HEAPU8.set(myTypedArray, buf).
Questions:
- What is the correct way to do this?
- Failing that, am I at least doing this correctly from a JavaScript-to-C perspective? Is the problem more likely with how C# represents byte arrays?