C# 你能从iOS C插件写入Unity纹理吗;“当场”吗;?

C# 你能从iOS C插件写入Unity纹理吗;“当场”吗;?,c#,ios,unity3d,opengl-es-2.0,unity3d-native-plugins,C#,Ios,Unity3d,Opengl Es 2.0,Unity3d Native Plugins,假设你有一个用于iOS的低级Unity插件 在c中也是如此# 创建一个纹理,.Apply(),然后将指针发送到本机iOS插件: public void ClickPassTexture() { tex = new Texture2D(256, 256, TextureFormat.ARGB32, false); tex.filterMode = FilterMode.Point; tex.Apply(); // ACTUALLY UPLOAD TO GPU

假设你有一个用于iOS的低级Unity插件

在c中也是如此#

创建一个纹理,
.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);
但是没有!(如果在
.Apply
之前等待几帧,则没有区别)

您可能认为
.Apply
会再次将纹理发送到gpu,但它似乎不起作用

简而言之,在C插件运行后,如何处理呈现的代码,使其显示新的纹理“there and then”?

当你知道一个纹理已经被一个C插件修改了,这在iOS上,如何让Unity显示修改??

你需要调用这个方法

更新Unity纹理以使用不同的本机纹理对象

此函数对于创建 平台特定的纹理对象以外的统一,并需要使用 这些纹理出现在Unity场景中。对于使用创建的纹理 CreateExternalTexture,此函数将切换到另一个底层 纹理对象(如果/当其更改时)

“新”统一中的解决方案 来自统一日本(Unity Japan)的(传奇)高桥惠次郎(Keijiro Takahashi)发布了可能是统一有史以来最重要的一件事:

从插件更新(new)逐帧样式纹理的实际示例

并与IOS配合使用

再一次,可以说这几乎是从统一中发出的最有价值的东西!呸



关于所问的问题,我真的没有找到解决办法。

我认为
从统一设置纹理(tex.GetNativeTexturePtr(),tex.width,tex.height);//。。。tex.Apply()将起作用。所以
tex=set_texture_from_unity(…);tex.Apply()因为Unity目前不知道它在应用什么。(虽然我相信
glTexSubImage2D
应该已经在你的C部分应用了它…)嗯,它肯定是同一个纹理-你传递指针,你知道吗?g_TextureHandlei如果通过代码在Unity中创建纹理。。您可以在本机插件中进行更改。。然而,似乎没有办法改变现有的纹理(由Unity从您的资产中加载的纹理)。@remy_rm!顺便说一句,请不要错过大师t的批判性文章-请参阅我在我的答案中编辑的链接!它已经在那里6个多月了,遗憾的是我没有注意到它。好了,T大师真是太好了,是吗?他是难以置信的帮助-这家伙修复了一个小错误在其中,字面上在几个小时内。太神了啊-看看吧,但我认为这是如果你想改变你的插件制作的“另一个”纹理。在本例中,插件正在更改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);
    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();