- Home /
Pass C# struct containing Matrix to C++ dll
I have the following structure in C# which mimics a C++ structure that serves as a parameter to my DLL function:
[Serializable]
[StructLayout(LayoutKind.Sequential)]
struct Volume
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 12)]
public Vector3 mExtent;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 64)]
public Matrix4x4 mWorldToObject;
}
My C++ function is declared within the C# as
[DllImport(dllname), CallingConvention = CallingConvention.Cdecl)]
private static extern Vector3 ComputeCurl(Vector3 p, IntPtr v);
How do I fill a Volume
object with data and pass its pointer to the plugin (dll)?
I don't really understand the marshalling aspects of Unity's data types. I tried the following, and the matrix data in C++ isn't right (the vector is alright weirdly..)
Volume obstacle = new Volume();
obstacle.mExtent = new Vector3();
obstacle.mExtent = t.lossyScale * 0.5f;
obstacle.mWorldToObject = Matrix4x4.TRS(t.position, t.rotation, Vector3.one);
IntPtr ptr= Marshal.AllocHGlobal(Marshal.SizeOf(typeof(Volume)));
Marshal.StructureToPtr(obstacle, ptr, false);
Do I need to use float[] instead of Vector3
and Matrix4x4
and individually assign (gulp) each element?
In reality, I need to pass a pointer to an array of Volume
. Is there anything special I'd need to do besides allocating a larger buffer and incrementing the ptr
by Marshal.Sizeof(typeof(Volume))
?
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Win Store plugin causing internal compiler error 1 Answer
Breakpoints in MonoBehaviour code called from SmartFoxServer2X.dll cause Editor debugging hangs. 0 Answers
Use a Different DLL When Building Application? 1 Answer
Dll distribution of Conditionally Compiled project 0 Answers