屏幕截图预览冻结游戏几秒钟

时间:2015-12-03 14:56:05

标签: c# unity3d 2d

我在关卡结束时有截屏预览,但是当我显示它时,我的游戏冻结了一点(低fps几秒钟)。这是我用来加载屏幕截图并裁剪它的代码(用于一些预览示例)。请帮我解决这个滞后问题

private IEnumerator ShowScreeshotPreview()
{
    yield return 0;

    screenshotTexture = new Texture2D(2, 2);
    byte[] fileData = File.ReadAllBytes(destination);
    yield return 0;

    screenshotTexture.LoadImage(fileData);
    yield return 0;

    Rect croppedSpriteRect = new Rect();
    croppedSpriteRect.height = screenshotTexture.height;
    croppedSpriteRect.width = screenshotTexture.height;
    croppedSpriteRect.y = 0;
    croppedSpriteRect.x = 0;

    croppedTexture = new Texture2D((int)croppedSpriteRect.width, (int)croppedSpriteRect.height);
    yield return 0;

    croppedTexture.SetPixels(screenshotTexture.GetPixels((int)croppedSpriteRect.x, (int)croppedSpriteRect.y, (int)croppedSpriteRect.width, (int)croppedSpriteRect.height));
    yield return 0;

    croppedTexture.Apply(); // Memory leak occurs here
    yield return 0;

    screenshotPrview.mainTexture = croppedTexture;
    yield return 0;

    ScreenshotPreviewAnimator.SetBool("visible", true);
    screenshotLoaded = true;
}

0 个答案:

没有答案