我试图将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;
我尝试使用各种
RenderTextureFormat
和TextureFormat
,但是似乎没有任何效果! 最佳答案
我相信这是由您的渲染纹理格式调用引起的。之前也有类似的事情。
这部分代码指定默认的纹理格式,然后在当前执行的环境支持时更改默认格式(添加了我的注释)
//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);
因此,在某些系统(无论哪种格式)上,您都试图将Pixels()从一种纹理格式读取为另一种。这可能是造成您的问题的原因。
您还可以通过动态更改目标纹理的格式来解决此问题。因此,在第一部分中,您将其更改为:
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/