Question by
Crendel-Studios · Dec 03, 2015 at 02:58 PM ·
c#2d2d game
Screenshot preview freezes a game for a few seconds
I have a screenshot preview at the end of the level, but when I show it my game freezes for a bit(low fps for a few seconds). This is code I use to load screenshot and crop it(for a little preview sample). Please, help me to solve this lag issue
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;
}
Comment
Your answer
Follow this Question
Related Questions
Why won't my 2D Sprite Move? 1 Answer
2D AI movement 0 Answers
Detect whether a Rigidbody collided with a Trigger 2 Answers
Animation location problem! 0 Answers
Teleport 2D Character on TriggerEnter 2 Answers