假设您有适用于iOS的低级Unity插件,
所以在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);
有一个纹理,.Apply()
它,然后将指针发送到本机iOS插件:
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);
}
现在的C代码。
使用几条彩色线条制作纹理,并将其写入Unity纹理。
#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);
所以,在C代码中,我们似乎成功地修改了纹理tex
。
然而。
精致的Unity doco docs.unity3d.com/Manual/NativePluginInterface.html
给出了一个示例,其中C纹理更改由Unity渲染链中的回调调用。
然而。
什么时候想?
我认为这可能只是工作:
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();
但不是! (如果你在.Apply
之前等待几帧,那没有区别。)
你认为.Apply
会再次将纹理发送到gpu,但它似乎不起作用。
简而言之,如何处理所呈现的代码,使其在C插件运行后显示“那里然后”的新纹理?
当你知道一个C插件修改了一个纹理,这在iOS上,如何让Unity显示更改?
来自Unity Japan的(传奇)Keijiro Takahashi发布了Unity发布的最重要的一件事:
https://github.com/keijiro/TextureUpdateExample
插件中(新)框架式纹理更新的实际示例。
它与IOS合作
再说一次,可以说这是Unity发出的最有价值的东西!唷!
关于问题,我真的没有找到解决方案。
你需要调用UpdateExternalTexture方法。
更新Unity纹理以使用不同的本机纹理对象。
此函数主要用于在Unity之外创建特定于平台的纹理对象的本机代码插件,并且需要在Unity场景中使用这些纹理。对于使用CreateExternalTexture创建的纹理,此函数在/更改时切换到另一个基础纹理对象。