I’ll start by saying Unity has an awesome partial example of this:
http://unity3d.com/support/resources/example-projects/texture-plugins
So that you can update the Texture colors in the plugin:
void Update () {
UpdateTexture (m_PixelsHandle.AddrOfPinnedObject(), m_Texture.width, m_Texture.height, Time.time);
m_Texture.SetPixels (m_Pixels, 0);
m_Texture.Apply ();
}
which could potentially save you 500 ms of transferring data over the marshalling interface.
My problem is they only showed the C# half of the problem.
I want to be able to write to that m_Pixels in an unmanaged plugin.
I figure they did something like:
extern "C"
{
EXPORT_API void UpdateTexture(Color32* pixels, int width, int height, float time);
}
But I’m finding that if I write to the pixels in the unmanaged code, I just get all solid green.
So Unity if you could opine to how you’ve written to the pixels that would be awesome.
Also it seems crazy to be writing to the managed pixels so that Texture2D.SetPixels and Texture2D.Apply can just take another 400ms to send the data back to Unity’s unmanaged code.
Would it be possible to just expose the IntPtr or Color32* pointer to Unity’s unmanaged texture pixels?
I could shave another 200ms from my transfer time with that.
I’m not having any problem getting the pixel data from Unity when I do this. It’s just terribly expensive to get a structure by pointer, one index at a time.
//Debug.Log(string.Format("actualWidth={0}, actualHeight={1}", actualWidth, actualHeight));
for (int index = 0; index < m_colors.Length; ++index)
{
IntPtr iPtr = GetColorAtIndex(m_nativeMemory, index);
if (iPtr == IntPtr.Zero)
{
continue;
}
Color32 color = (Color32)Marshal.PtrToStructure(iPtr, typeof(Color32));
m_colors[index] = color;
}
elapsed = (float)(DateTime.Now - m_startTime).TotalMilliseconds;
Debug.Log(string.Format("Native transfer elapsed={0}", elapsed));
Thanks if you can share your C++ plugin interface for how you wrote to the pinned managed array from the plugin.