您可以从iOS C插件“当场"写入Unity纹理吗? [英] Can you write to a Unity texture, from an iOS C plugin, "on the spot"?

查看:162
本文介绍了您可以从iOS C插件“当场"写入Unity纹理吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设您有一个适用于iOS的低级Unity插件,

Say you have a low-level Unity plugin for iOS,

所以在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插件:

have a texture, .Apply() it, and then send the pointer to the native iOS plugin:

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纹理.

Make a texture with a few colored lines, and write it to the Unity texture.

#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.

So, in the C code, we have seemingly successfully modified the texture tex.

但是.

图片显示完全无法完成任何操作.

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

The fine Unity doco docs.unity3d.com/Manual/NativePluginInterface.html

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

gives an example where the C texture change gets called by a callback in the Unity rendering chain.

但是.

何时我们想要?

我认为这可能很简单:

    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之前等待几帧,这没有什么区别.)

But no! (It makes no difference if you wait a few frames before the .Apply.)

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

You'd think .Apply would send the texture up to the gpu again, but it seems to not work.

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

当您知道一个C插件已经修改了Texture时,如何在iOS上使用Unity来显示更改?

When you know a Texture has been modified by a C plugin, this on iOS, how to get Unity to display the change???

推荐答案

新" Unity中的解决方案

Unity Japan的(传奇人物)高桥敬次郎发表了有史以来Unity中最重要的一件事情:

Solution in the "new" Unity

The (legendary) Keijiro Takahashi from Unity Japan, has posted probably the single most important thing ever posted from Unity:

https://github.com/keijiro/TextureUpdateExample

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

它和IOS一起使用

AND IT WORKS WITH IOS

再说一遍,这几乎是Unity发行的最有价值的东西! !

Again, all that can be said is this pretty much the most valuable thing ever emitted from Unity! Phew!

关于所问的问题,我确实从未找到解决方法.

Regarding the question as asked, I really have never found the solution.

这篇关于您可以从iOS C插件“当场"写入Unity纹理吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆