Unity 压缩texture
Posted Lumious
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Unity 压缩texture相关的知识,希望对你有一定的参考价值。
当我们往服务器保存图片时
并不会仅仅保留原图
一般会另外保存一张缩略图
当加载文件夹时只加载缩略图
当在点击缩略图打开图片时
再加载原缩略图
以节省时间和内存
下面以将屏幕截图保存到服务器为例
将屏幕截图和截图缩略图保存到服务器
private IEnumerator UploadPNG(string id) { yield return new WaitForEndOfFrame(); int width = Screen.width; int height = Screen.height; //屏幕截图 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(); //缩略图(256*256) Texture2D texThumb = new Texture2D(256, 256); //压缩图片 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上传到服务器 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 压缩texture的主要内容,如果未能解决你的问题,请参考以下文章