代码之家  ›  专栏  ›  技术社区  ›  Fattie

你能写一个统一的纹理,从一个iOS C插件,“现场”?

  •  2
  • Fattie  · 技术社区  · 6 年前

    假设你有一个用于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 .

    然而。

    它不会出现在屏幕上。

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

    美好的统一文档 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 )

    你会想 应用 会再次将纹理发送到GPU,但似乎不起作用。

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

    当你知道一个纹理已经被一个c插件修改,这个在ios上,如何得到unity来显示修改????

    1 回复  |  直到 6 年前
        1
  •  2
  •   Fattie    6 年前

    “新”统一的解决方案

    来自日本统一的(传说中的)高桥庆二郎(Keijiro Takahashi)可能发布了Unity有史以来最重要的一条消息:

    https://github.com/keijiro/TextureUpdateExample

    实例 的( 新的 )从插件更新帧样式纹理。

    它与ios一起工作

    同样,所有可以说的是这是从团结中发出的最有价值的东西!唷!


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

        2
  •  1
  •   Hamid Yusifli    6 年前

    你需要打电话 UpdateExternalTexture 方法。

    更新Unity纹理以使用不同的原生纹理对象。

    此函数主要用于创建 平台特定的纹理对象外部统一,需要使用 统一场景中的这些纹理。对于使用创建的纹理 createExternalTexture,此函数切换到另一个底层 纹理对象发生变化时。