Skip to content

Instantly share code, notes, and snippets.

@romanus
Created December 1, 2017 09:08
Show Gist options
  • Save romanus/e39d15f7b6559327242de61711065d65 to your computer and use it in GitHub Desktop.
Save romanus/e39d15f7b6559327242de61711065d65 to your computer and use it in GitHub Desktop.
// The interop signature in the Unity script.
[DllImport("mypluginname")]
private static extern bool getSomeArrayData(ref IntPtr ptrResultVerts, ref int resultVertLength);
// An example of calling the interop function.
public MyMarshallingMethod()
{
IntPtr ptrResultVerts = IntPtr.Zero;
int resultVertLength = 0;
bool success = getSomeArrayData(ref ptrResultVerts, ref resultVertLength)
float[] resultVertices = null;
if (success)
{
// Load the results into a managed array.
resultVertices = new float[resultVertLength];
Marshal.Copy(ptrResultVerts
, resultVertices
, 0
, resultVertLength);
/*
* WARNING!!!! IMPORTANT!!!
* In this example the plugin created an array allocated
* in unmanged memory. The plugin will need to provide a
* means to free the memory.
*/
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment