Hi,
I need to save thumbnails and then be able to load them back in a GUI.
I figured the reading part Ok, using WWW for reading from a file, using a “file://” type of url.
Right now, I’m using the code from the doc to read from the screen and save to a png file, which I load back in the GUI later.
http://unity3d.com/support/documentation/ScriptReference/Texture2D.EncodeToPNG.html
Now, to lighten storage and free some video memory, I’d like to resize those thumbnails prior to saving them.
I am trying the following, with no luck :
function SaveSmallThumbnail () {
var path = “test.png”;
if (path)
{
yield WaitForEndOfFrame();
// Create a texture the size of the screen, RGB24 format
var width = Screen.width;
var height = Screen.height;
var tex = new Texture2D (width, height, TextureFormat.RGB24, false);
// Read screen contents into the texture
tex.ReadPixels (Rect(0, 0, width, height), 0, 0);
tex.Apply ();
tex.Resize(tex.width/2,tex.height/2,TextureFormat.RGB24, false); // this screws the saved image, why ?
tex.Apply ();
// Encode texture into PNG
var bytes = tex.EncodeToPNG();
// Destroy (tex);
File.WriteAllBytes(path, bytes);
}
}
For a reason that I don’t get, when I use the resize function, the saved image turns black, or pixel jam.
I someone can provide at least an explanation, I’d be glad to read it.
If someone has a solution, even better. Btw, I’d like to keep the aspect ratio, and crop to the smallest dimension of the image.
Something tells me I’ll end writing a custom function…