gpt4 book ai didi

c# - 您可以从 iOS C 插件 "on the spot"写入 Unity 纹理吗?

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:53:07 25 4
gpt4 key购买 nike

假设您有一个适用于 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 图像显示完全没有实现任何目标。

优秀的 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 插件运行后,如何处理呈现的代码,使其“立即”显示新纹理??

当你知道一个 Texture 已经被 C 插件修改时,在 iOS 上,如何让 Unity 显示更改???

最佳答案

"new"Unity 中的解决方案

来自 Unity Japan 的(传奇)Keijiro Takahashi 发布了可能是 Unity 发布的最重要的内容:

https://github.com/keijiro/TextureUpdateExample

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

它适用于 IOS

再次强调,这几乎是 Unity 发出的最有值(value)的东西!呸!


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

关于c# - 您可以从 iOS C 插件 "on the spot"写入 Unity 纹理吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54189957/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com