Say you have a low-level Unity plugin for iOS,
So in c#
using System.Runtime.InteropServices;
using AOT;
public class Teste: MonoBehaviour {
[DllImport("__Internal")] private static extern
void set_texture_from_unity(System.IntPtr texture, int w, int h);
have a texture, .Apply()
it, and then send the pointer to the native iOS plugin:
public void ClickPassTexture() {
tex = new Texture2D(256, 256, TextureFormat.ARGB32, false);
tex.filterMode = FilterMode.Point;
tex.Apply(); // ACTUALLY UPLOAD TO GPU
someMaterial.mainTexture = tex;
set_texture_from_unity(
tex.GetNativeTexturePtr(), tex.width, tex.height);
}
Now the C code.
Make a texture with a few colored lines, and write it to the Unity texture.
#include <OpenGLES/ES2/gl.h>
...
void set_texture_from_unity(void *g_TextureHandle, int w, int h)) {
// make a texture with a few gray rows
unsigned char* data = malloc( g_TextureWidth * 4 * g_TextureHeight );
for (int i = 0; i < 1000; ++i) { data[i] = 100; }
// now, write that to the texture pointer given to us from Unity
GLuint gltex = (GLuint)(size_t)(g_TextureHandle);
glBindTexture(GL_TEXTURE_2D, gltex);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0,
g_TextureWidth, g_TextureHeight, GL_RGBA, GL_UNSIGNED_BYTE, data);
free(data);
So, in the C code, we have seemingly successfully modified the texture tex
.
However.
It does not appear on screen.
Image shows complete failure to achieve anything at all.
The fine Unity doco docs.unity3d.com/Manual/NativePluginInterface.html
gives an example where the C texture change gets called by a callback in the Unity rendering chain.
However.
Can we simply make Unity take the new texture "now"?
When we want to ?
I thought this might simply work:
set_texture_from_unity( tex.GetNativeTexturePtr(),
tex.width, tex.height);
// the plugin has changed the texture...
// perhaps an Apply will change it on screen?!
tex.Apply();
But no! (It makes no difference if you wait a few frames before the .Apply
.)
You'd think .Apply
would send the texture up to the gpu again, but it seems to not work.
In short, what to do to the presented code, to make it show the new texture "there and then", after the C plugin runs??
When you know a Texture has been modified by a C plugin, this on iOS, how to get Unity to display the change???