2

Have a C++ DLL that produces a series of contours (a couple of connected points) provided through a single GChandle "pinned" array. Now I need to separate each contour into its own C# array to use them in an engine function (the engine being Unity). Sure I can do this by copying, but as this is fairly performance critical, I'm hoping for a more direct solution.

Is it possible to get an array that points to a selected index from larger vector? Like an equivalent to the following C++ code (here done with ints):

int* large_array = new int[1000];
int* sub_array = &large_array[50];

std::cout << (large_array[70] == sub_array[20]); // <-- True

Huge thanks in advance :)

DragonGamer
  • 834
  • 3
  • 9
  • 27
  • are you passing the arrays to this unity function, or just need to use them inside the function? This is possible by compiling "unsafe" and by using int* to point to `Marshal.UnsafeAddrOfPinnedArrayElement` I am not sure if compiling with unsafe is possible with Unity programming though. – Oguz Ozgul Apr 25 '20 at 08:17
  • https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/unsafe-code-pointers/pointer-types – Deepak Mishra Apr 25 '20 at 08:18
  • @OguzOzgul Unfortunately Unity does directly require the array and not an unsafe pointer. I do not only want to work with the data manually. It also does not accept ArraySegment. So guess there is no way to avoid copying due to Unity's limitation. Perhaps I could have a pool of preset arrays and pass them all to the DLL for it to fill them adequately, but that really would be high effort for premature optimization. Thanks for the answer nevertheless! – DragonGamer Apr 25 '20 at 09:01

0 Answers0