首页 > 代码库 > Unity 压缩texture
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
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。