繁体   English   中英

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

[英]Can you write to a Unity texture, from an iOS C plugin, “on the spot”?

假设您有适用于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渲染链中的回调调用

然而。

我们可以简单地让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

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

https://github.com/keijiro/TextureUpdateExample

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

它与IOS合作

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


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

您需要调用UpdateExternalTexture方法。

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM