调用Texture2D.readPixels时iOS挂起

时间:2016-09-13 21:26:53

标签: c# ios unity3d texture2d

我正在尝试将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,但似乎没有任何效果!

1 个答案:

答案 0 :(得分:1)

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

这段代码分配默认纹理格式,如果当前正在执行的环境支持默认格式,则会改变默认格式(我的评论已添加)

//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);

因此,在某些系统上(无论哪种格式支持),您尝试将Pix()从一种纹理格式读取到另一种纹理格式。这可能会导致您的问题。

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

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);

如果您在评论中仍有问题,请与我们联系。