Hello gurus!
I am creating a Unity dll and I need Unity to access the memory buffer I must create in the dll as an array of integers in Unity C# script.
I have played with GCHandle functions but have not been thus far been able to make it work (c# - GCHandle to get address(pointer) of .net object - Stack Overflow)
Anybody knows how to do this?
Many thanks!!
Dan
try Marshal.Copy(…), you will copy your memory content as a param typed IntPtr into your target array
Hi Xiewneqi, thanks very much for taking the time.
I tried Marshal.Copy() but it was not able to restore to an array of Vector3(). I also want to avoid doing a memcpy as much as possible as this code needs to run each frame!
After much experimentation, the ‘least bad’ thing that worked was to 1) first pin the C# object in memory with a line like:
_hVerts = GCHandle.Alloc(_aVerts, GCHandleType.Pinned);
-
then I use _hVerts.AddrOfPinnedObject() to my C++ dll.
-
then (once per frame) I call a standard memcpy from my C++ allocated global buffer to the IntPtr Unity gives me.
Unfortunately, I cannot use the buffer Unity gives me to cross process boundaries (like I can with a buffer created with CreateFileMapping() and MapViewOfFile() that I can read/write in another process.
So in order to avoid memcpy at every frame I need Unity to use my dll-allocated buffer for that Vector3 array.
Does anyone know when creating a C# object how to tell Unity to use an IntPtr instead of allocating from memory in a place in memory I can’t use accross processes?
Many thanks for any hint you can give!!
Dan,