- Home /
Passing Arrays as Parameters Between Functions in C# and C++ dll's within Unity
Here's the sample code where the external C++ function is declared in the C# code:
[DllImport("PhysicsEngine")]
private static extern void CalculateTrajectory(float deltaT, float muzzleVelocity, float theta, float phi, float[] origin, float[,] trajectory, int size);
And, here's where the function is called:
int size = 20;
float deltaT = 0.02f;
float muzzleVelocity = 900.0f;
float[] origin = new float[3];
origin[0] = -200.0f;
origin[1] = 1.242f;
origin[2] = 1000.0f;
float theta = 0.0f;
float phi = 0.0f;
float maxRange = 1000.0f;
float[,] trajectory = new float[size, 3];
CalculateTrajectory(deltaT, muzzleVelocity, theta, phi, origin, trajectory, size);
Here's the original C++ function definition's name and parameters:
void EXPORT_API CalculateTrajectory(float deltaT, float muzzleVelocity, float theta, float phi, float* origin, float** trajectory, int size)
The problem is that when I try to run the game in Unity or my own personal C# tester program, both programs crash. Why is that? Has anyone else successfully passed arrays as parameters between Unity and their own C++ dll's? If so, then please let me know so that I and others may benefit from your knowledge. Thank-you.
Answer by SteveFSP · Dec 02, 2010 at 03:43 AM
The answer I gave to your other question has some links which may be helpful.
There is a link to an MSDN page which discusses marshaling arrays. Also, there is a link to some code I use to pass arrays back and forth between Unity and a C++ plugin. (Though I haven't attempted multi-dimensional arrays.)
From experience, I strongly recommend testing all marshaling code outside Unity in an IDE. Once it is working there without any errors, then move it into Unity. You can avoid a lot of the hassle since your external IDE will usually give you better error responses than Unity can provide.
Your answer
Follow this Question
Related Questions
Monodevelop Go to declaration bug 3 Answers
MonoDevelop 2.8.2 Crash 1 Answer
Can I prevent MonoDevelop's 'Find in Files' from crashing? 2 Answers
Can't find MonoDevelop. Unity 4.5.2 1 Answer