I’m trying to create a plugin that lets you grab images from the ALAssetLibrary in the photo album. I’ve made pretty good progress considering I’m very new to objective c. The specific problem I’m having now is that I have the bytes for a jpeg, and I just need to return them back to my c# script.
Thanks for the reply, but would prefer to go a simpler route. Here is what I’ve managed to put together so far, but I’m now getting a EXC_BAD_ACCESS. Heres the code so far:
int _testArray(int* dataPtr)
{
int len = 5;
unsigned char* data = (unsigned char*)malloc(len);
for (int i = 0; i < len; i++)
data[i] = i;
*dataPtr = (int)data;
return len;
}
C#
[DllImport ("__Internal")]
private static extern int _testArray(out IntPtr p);
void Start()
{
IntPtr unmanagedPtr;
int size = _testArray(out unmanagedPtr);
byte[] mangedData = new byte[size];
Marshal.Copy(unmanagedPtr, mangedData, 0, size);
for (int i = 0; i < size; i++)
print(string.Format("managedData[{0}]:{1}", i, mangedData[i]));
//don't forget to free the unmanaged memory
Marshal.FreeHGlobal(unmanagedPtr);
}
I got it working on Unity 2019.4.7 by changing the native argument type to be a pointer to int array instead of pointer to int:
Obj-C
// int* is an array, int** is pointer to array.
int GetBytes(int** dataPtr)
{
int len = 5;
unsigned char* data = (unsigned char*)malloc(len);
for (int i = 0; i < len; i++)
data[i] = i;
// Don't care about array type; cast to same pointer type.
*dataPtr = (int*)data;
return len;
}
C#
[DllImport("PluginName", CallingConvention = CallingConvention.Cdecl)]
private static extern int GetBytes(out IntPtr p);
void Start()
{
IntPtr unmanagedPtr;
int len = GetBytes(out unmanagedPtr);
byte[] mangedData = new byte[len];
Marshal.Copy(unmanagedPtr, mangedData, 0, len);
for (int i = 0; i < len; i++)
Debug.Log(string.Format("managedData[{0}]:{1}", i, mangedData[i]));
Marshal.FreeHGlobal(unmanagedPtr);
}