1

I wrote a wrapper to access a C++ Library from Unity game engine using C#. It works for the most part, but running into multiple issues when trying to pass an array as a parameter from C++ to C#.

I've tried declaring them as [MarshalAs(UnmanagedType.SafeArray or [MarshalAs(UnmanagedType.LPArray but I keep getting runtime errors saying I can't access Unmanaged code from C#.

I ended up implementing a fixed structure with each of the array sizes I need:

struct FloatArray6Dof{
float data[6];};


Func<FloatArray6Dof, bool> del = new Func<FloatArray6Dof, bool>((FloatArray6Dof f) =>
        {
            unsafe
            {
                VectorN pose = new VectorN(6);
                for (int i = 0; i < pose.Dof; i++)
                {
                    pose[i] = f.Array[i];
                }
                //use pose
            }
        });

However, a lot more "unnecesary" code is needed for this and it does not work for dynamically size arrays.

For passing an dynamic size array I implemented a function PassValue(float value) which C++ calls in a loop passing each of the elements of the array. This works, but it's not very efficient.

Any ideas on how to correctly use Unmanaged arrays from C++ in Unity on C#?

0 Answers0