I have a C++ library method that I'd like to call from a Unity C# script.
I understand there are three key steps. First, to declare the C++ methods to be extern "C"
. Second, to use [DllImport('foobar')]
before the corresponding C# extern method declaration. Third, to name the library file with the lib prefix (e.g. libfoobar.so
) and place it in the Unity plugins folder.
So far so good -- if I'm only passing simple parameter types like int
from C# into C++. However, to pass a byte[]
parameter, I will need to account for the different ways C# and C++ handle memory and pointers. I haven't been able to find a definitive example of how to this should be done.
My question: how to pass a byte[]
from a Unity C# script into a external C++ library method?