This is my first time going deeper into manipulating Textures in Unity.
I am attempting to use Graphics.Blit to resize a Texture2D after seeing used in such a manner in posts like this one. It seems to mostly work, but for some reason, leaves a line of pixels on part of the edge of the resized texture, and I cannot for the life of me figure out why.
Here is the resizing function.
Texture2D ResizeTexture2D(Texture2D originalTexture, int resizedWidth, int resizedHeight)
{
RenderTexture renderTexture = new RenderTexture(resizedWidth, resizedHeight, 32, RenderTextureFormat.ARGB32);
RenderTexture.active = renderTexture;
Graphics.Blit(originalTexture, renderTexture);
Texture2D resizedTexture = new Texture2D(resizedWidth, resizedHeight);
resizedTexture.ReadPixels(new Rect(0, 0, resizedWidth, resizedHeight), 0, 0);
resizedTexture.Apply();
return resizedTexture;
And here is my call to said function.
float percentShrink = 0.30f;
Texture2D shrunkTexture = ResizeTexture2D(cleanedPiece, (int)(cleanedPiece.width * percentShrink), (int)(cleanedPiece.height * percentShrink));
For the purpose of sharing the output, I save it as a png with this.
System.IO.File.WriteAllBytes(System.IO.Path.Combine(saveToDirectory, tex.name + ".png"), tex.EncodeToPNG());
This leaves me with my original full size image
And the 30% smaller image with the artifacts. (the lines on the edges of the image)
You’ll notice that it’s a puzzle piece. The same thing occurs with other pieces, but not consistently. Sometimes a side will have it, while on a different piece, it won’t. For instance, the piece directly beneath this one looks like this after the resize.
If I’m doing something wrong with my implementation, or if you know of a better way to resize a Texture2D, please let me know. I’m kind of at my wits end here.