你能从iOS C插件“现场”写一个Unity纹理吗?

问题描述 投票:3回答:2

假设您有适用于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);
}

enter image description here

现在的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

然而。

It does not appear on screen.

enter image description here图像显示完全无法实现任何目标。

精致的Unity doco docs.unity3d.com/Manual/NativePluginInterface.html

给出了一个示例,其中C纹理更改由Unity渲染链中的回调调用。

然而。

Can we simply make Unity take the new texture "now"?

什么时候想?

我认为这可能只是工作:

    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显示更改?

c# ios unity3d opengl-es-2.0 unity3d-native-plugins
2个回答
2
投票

Solution in the "new" Unity

来自Unity Japan的(传奇)Keijiro Takahashi发布了Unity发布的最重要的一件事:

https://github.com/keijiro/TextureUpdateExample

插件中(新)框架式纹理更新的实际示例。

它与IOS合作

再说一次,可以说这是Unity发出的最有价值的东西!唷!


关于问题,我真的没有找到解决方案。


1
投票

你需要调用UpdateExternalTexture方法。

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

此函数主要用于在Unity之外创建特定于平台的纹理对象的本机代码插件,并且需要在Unity场景中使用这些纹理。对于使用CreateExternalTexture创建的纹理,此函数在/更改时切换到另一个基础纹理对象。

© www.soinside.com 2019 - 2024. All rights reserved.