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.
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.
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???
The (legendary) Keijiro Takahashi from Unity Japan, has posted probably the single most important thing ever posted from Unity:
https://github.com/keijiro/TextureUpdateExample
An actual example of the (new) frame-wise style texture-updating from plugin.
AND IT WORKS WITH IOS
Again, all that can be said is this pretty much the most valuable thing ever emitted from Unity! Phew!
Regarding the question as asked, I really have never found the solution.