I’m using the screenshot script off the wiki which works fine for saving a screenshot, but I’m having trouble resizing it. (I know Texture2d.Resize loses the data so I’m trying to just save it to a new texture2d the size I want it)
I want to take a screenshot and resize it to 256x256 pixels. Actually, I want to take several screenshots, resize them and pack them into a single texture but I figured taking the screenshot and resizing it is the first step.
Texture2D texture = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, false);
Texture2D textureSmall = new Texture2D(256, 256, TextureFormat.RGB24, false);
texture.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0);
texture.Apply();
textureSmall.SetPixels (0,0,256,256,texture.GetPixels ());
textureSmall.Apply ();
byte[] bytes = texture.EncodeToPNG();
byte[] bytes2 = textureSmall.EncodeToPNG();
File.WriteAllBytes(Application.dataPath + "/../testscreennn-" + count + ".png", bytes2);
File.WriteAllBytes(Application.dataPath + "/../testscreen-" + count + ".png", bytes);
count++;
DestroyObject( texture );
DestroyObject( textureSmall );
I tried a few other things, like this function:
ScaleTexture(texture, 256, 256);
to call:
private Texture2D ScaleTexture(Texture2D source,int targetWidth,int targetHeight) {
Texture2D result=new Texture2D(targetWidth,targetHeight,source.format,true);
Color[] rpixels=result.GetPixels(0);
float incX=((float)1/source.width)*((float)source.width/targetWidth);
float incY=((float)1/source.height)*((float)source.height/targetHeight);
for(int px=0; px<rpixels.Length; px++) {
rpixels[px] = source.GetPixelBilinear(incX*((float)px%targetWidth),
incY*((float)Mathf.Floor(px/targetWidth)));
}
result.SetPixels(rpixels,0);
result.Apply();
return result;
}
With no success.
Any tips?