gpt4 book ai didi

c# - 调用 Texture2D.readPixels 时 iOS 挂起

转载 作者:行者123 更新时间:2023-11-29 00:41:12 29 4
gpt4 key购买 nike

我试图将 RenderTexture 绘制到 Texture2D 中,目的是将其保存到磁盘。这种方法在 OSX 编辑器和 Android 上一直有效。

我在 XCode 控制台中没有看到任何错误,当我调用 Texture2D.ReadPixels() 时,我的应用程序变得完全卡住

这里是代码的总结:

    // declaring variables...
RenderTexture outputTexture;
RenderTextureFormat RTFormat = RenderTextureFormat.ARGB32;

// use an appropriate format for render textures
if(SystemInfo.SupportsRenderTextureFormat(RenderTextureFormat.ARGBFloat)){
RTFormat = RenderTextureFormat.ARGBFloat;
}else if(SystemInfo.SupportsRenderTextureFormat(RenderTextureFormat.ARGBHalf)){
RTFormat = RenderTextureFormat.ARGBHalf;
}

// create instance of output texture
outputTexture = new RenderTexture (res.x, res.y, 0, RTFormat);

// in Update, draw stuff to outputTexture
Graphics.Blit (outputTexture, canvasTexture);
Graphics.Blit (canvasTexture, outputTexture, material);

// later... user wants to save the image
// draw rendertexture to a Texture2D so we can write to disk
RenderTexture.active = outputTexture;
tmpTexture = new Texture2D (outputTexture.width, outputTexture.height, TextureFormat.ARGB32, false);
tmpTexture.ReadPixels (new Rect (0, 0, outputTexture.width, outputTexture.height), 0, 0, false);
tmpTexture.Apply ();
RenderTexture.active = null;

我尝试过使用各种RenderTextureFormatTextureFormat,但似乎没有任何效果!

最佳答案

我相信这是由您的渲染纹理格式调用引起的。之前也发生过类似的事情。

这段代码分配默认纹理格式,然后在当前执行环境支持的情况下更改默认格式(添加了我的注释)

//set a default render texture of  RenderTextureFormat.ARGB32;
RenderTextureFormat RTFormat = RenderTextureFormat.ARGB32;

// if my system supports it, switch to either ARGBFloat or ARGBHalf

// use an appropriate format for render textures
if(SystemInfo.SupportsRenderTextureFormat(RenderTextureFormat.ARGBFloat)){
RTFormat = RenderTextureFormat.ARGBFloat;
}else if(SystemInfo.SupportsRenderTextureFormat(RenderTextureFormat.ARGBHalf)){
RTFormat = RenderTextureFormat.ARGBHalf;
}

但是,当您稍后实际定义临时纹理以填充 ReadPixels() 时,您只能以一种方式定义它(同样,我添加了评论)

//define a new tmpTexture container, ALWAYS with a TextureFormat of ARGB32
tmpTexture = new Texture2D (outputTexture.width, outputTexture.height, TextureFormat.ARGB32, false);

因此,在某些系统(无论支持该格式的系统)上,您尝试将 readPixels() 从一种纹理格式转换为另一种纹理格式。这可能是导致您出现问题的原因。

您还可以通过动态更改目标纹理的格式来解决此问题。因此,在第一部分中,您将其更改为:

RenderTextureFormat RTFormat = RenderTextureFormat.ARGB32;

//add another variable here for the destination Texture format
var destinationFormat = TextureFormat.ARGB32;


// use an appropriate format for render textures
if(SystemInfo.SupportsRenderTextureFormat(RenderTextureFormat.ARGBFloat)){
RTFormat = RenderTextureFormat.ARGBFloat;

//also set destination format
destinationFormat = TextureFormat.RGBAFloat;
}else if(SystemInfo.SupportsRenderTextureFormat(RenderTextureFormat.ARGBHalf)){
RTFormat = RenderTextureFormat.ARGBHalf;

//also set destination format
destinationFormat = TextureFormat.RGBAHalf;
}

当然,稍后在声明目标对象时使用动态设置的格式:

//define a new tmpTexture container, with a dynamically set destination format that always matches the input texture
tmpTexture = new Texture2D (outputTexture.width, outputTexture.height, destinationFormat, false);

如果您在评论中仍有问题,请告诉我。

关于c# - 调用 Texture2D.readPixels 时 iOS 挂起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39479245/

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