- Home /
Android: Alter texture from c++
Hi, I would like to alter a Texture2D from C++ on Android. How do I pass the texture to C++ side? Can I just alter it with OpenGL afterwards and my changes will be displayed or will I have to call some Unity function after my work is done to apply the changes?
Answer by liortal · Jul 16, 2016 at 10:50 AM
You should check out this method: https://docs.unity3d.com/ScriptReference/Texture.GetNativeTexturePtr.html
This gives you a pointer to the native texture (e.g: OpenGL) so you can work with this pointer in your native C++ code.
*Note that on other platforms this may be a different API (Metal on iOS and OSX).
Thank you, this seems like the thing I need. One more thing though: Do I have to somehow synchronise my edits with unity or can I draw whatever I want whenever I want and next time the scene is rendered my changes appear on the surface? I am completely new to Unity and do not really know how the ti$$anonymous$$gs are (If you can point me to any reference on this it would help me a lot as well!).
I have no idea as i've never used this approach. I guess the circumstances where one should need something like this are pretty rare (what is your scenario?)
I guess I will have to see if it refreshes automatically. I need this to display the preview from an external USB camera. I have written a library to access V4L2 cameras in C++, and I want the preview to display on a Unity surface. I guess the best way to do this is to store the raw images from the camera on C++ side and then load them onto the surface when Update() is called on my surface. The other idea would have been to load the images onto the surface each time an image is sent by the camera, gaining a few microseconds of delay, but I assume the safer way is to use Surface2D's LoadImage on Update().