Unity Compression Texture

Source: Internet
Author: User

When we save the picture to the server

Doesn't just keep the original.

A thumbnail image is usually saved separately

Load thumbnails only when folders are loaded

When you click the thumbnail to open the picture

Reload the original thumbnail image

To save time and memory

The following is an example of saving a screen to a server

Save the screen and thumbnails to the server

    Private IEnumerator uploadpng (string id) {yield return new waitforendofframe ();        int width = screen.width;        int height = screen.height;        Screen texture2d tex = new texture2d (width, height, textureformat.argb32, false); Tex.        Readpixels (New Rect (0, 0, width, height), 0, 0); Tex.        Apply (); byte[] bytes = Tex.        Encodetopng ();                Thumbnail (256*256) texture2d texthumb = new Texture2d (256, 256);        Compress picture color[] Destpix = new color[texthumb.width * Texthumb.height];        float warpfactor = 1.0f;        int y = 0;            while (Y < texthumb.height) {int x = 0;                while (x < texthumb.width) {float Xfrac = x * 1.0F/(TEXTHUMB.WIDTH-1);                float Yfrac = y * 1.0F/(TEXTHUMB.HEIGHT-1);                float Warpxfrac = Mathf.pow (Xfrac, warpfactor);                float Warpyfrac = Mathf.pow (Yfrac, warpfactor); Color C0 = Tex. GEtpixelbilinear (Warpxfrac, Warpyfrac);                Xfrac = (x-1) * 1.0F/(TEXTHUMB.WIDTH-1);                Yfrac = y * 1.0F/(TEXTHUMB.HEIGHT-1);                Warpxfrac = Mathf.pow (Xfrac, warpfactor);                Warpyfrac = Mathf.pow (Yfrac, warpfactor); Color C1 = Tex.                Getpixelbilinear (Warpxfrac, Warpyfrac);                Xfrac = (x + 1) * 1.0F/(TEXTHUMB.WIDTH-1);                Yfrac = y * 1.0F/(TEXTHUMB.HEIGHT-1);                Warpxfrac = Mathf.pow (Xfrac, warpfactor);                Warpyfrac = Mathf.pow (Yfrac, warpfactor); Color C2 = Tex.                Getpixelbilinear (Warpxfrac, Warpyfrac);                Xfrac = (x + 1) * 1.0F/(TEXTHUMB.WIDTH-1);                Yfrac = (y-1) * 1.0F/(TEXTHUMB.HEIGHT-1);                Warpxfrac = Mathf.pow (Xfrac, warpfactor);                Warpyfrac = Mathf.pow (Yfrac, warpfactor); Color C3 = Tex.                Getpixelbilinear (Warpxfrac, Warpyfrac); Xfrac = (x-0) * 1.0F/(texthUMB.WIDTH-1);                Yfrac = (y-1) * 1.0F/(TEXTHUMB.HEIGHT-1);                Warpxfrac = Mathf.pow (Xfrac, warpfactor);                Warpyfrac = Mathf.pow (Yfrac, warpfactor); Color C4 = Tex.                Getpixelbilinear (Warpxfrac, Warpyfrac);                Xfrac = (x-1) * 1.0F/(TEXTHUMB.WIDTH-1);                Yfrac = (y-1) * 1.0F/(TEXTHUMB.HEIGHT-1);                Warpxfrac = Mathf.pow (Xfrac, warpfactor);                Warpyfrac = Mathf.pow (Yfrac, warpfactor); Color C5 = Tex.                Getpixelbilinear (Warpxfrac, Warpyfrac);                Xfrac = (x + 1) * 1.0F/(TEXTHUMB.WIDTH-1);                Yfrac = (y + 1) * 1.0F/(TEXTHUMB.HEIGHT-1);                Warpxfrac = Mathf.pow (Xfrac, warpfactor);                Warpyfrac = Mathf.pow (Yfrac, warpfactor); Color C6 = Tex.                Getpixelbilinear (Warpxfrac, Warpyfrac);                Xfrac = (x-0) * 1.0F/(TEXTHUMB.WIDTH-1); Yfrac = (y + 1) * 1.0F/(texthumb.height-1);                Warpxfrac = Mathf.pow (Xfrac, warpfactor);                Warpyfrac = Mathf.pow (Yfrac, warpfactor); Color c7 = Tex.                Getpixelbilinear (Warpxfrac, Warpyfrac);                Xfrac = (x-1) * 1.0F/(TEXTHUMB.WIDTH-1);                Yfrac = (y + 1) * 1.0F/(TEXTHUMB.HEIGHT-1);                Warpxfrac = Mathf.pow (Xfrac, warpfactor);                Warpyfrac = Mathf.pow (Yfrac, warpfactor); Color C8 = Tex.                Getpixelbilinear (Warpxfrac, Warpyfrac);  Color cr = C0 * 0.25f + C1 * 0.125f + c2 * 0.125f + c3 * 0.0625f + c4 * 0.125f + c5 * 0.0625f + c6 * 0.0625f + c7 * 0.125f                + C8 * 0.0625F;                Destpix[y * texthumb.width + x] = CR;            x + +;        } y++;        } texthumb.setpixels (Destpix);        Texthumb.apply (); byte[] Bytesthumb = Texthumb.encodetopng ();
Destroy (Tex); Destroy (TEXTHUMB);
Wwwform upload to server wwwform form = new Wwwform (); Form. AddField ("cmd", "Savedraw"); Form. Addbinarydata ("screenshot", Bytesthumb); Form. Addbinarydata ("Draw", bytes); www w = new www (url, form); Yield return w; if (w.error! = null) {Debug.Log ("error:" + w.error); } else Debug.Log ("OK"); }

Unity Compression Texture

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.