using System.Collections; using System.Collections.Generic; using UnityEngine; using System.IO; using System; using GameCore; public class CompressManager : UnitySingleton { /// /// Compresses the picture. /// /// Image path. /// 成功返回原地址,失败返回null. public void CompressPicture(int compressSize, string sourcePath, string outPath, Action complete) { StartCoroutine(Compress(compressSize, sourcePath, outPath, delegate(bool isSucess) { if (complete != null) complete(isSucess); })); } IEnumerator Compress(int compressSize, string sourcePath, string outPath, Action action) { int count = 0; while (!File.Exists(sourcePath)) { yield return new WaitForSeconds(0.1f); count++; if (count >= 10) { if (action != null) action(false); yield break; } } FileInfo f = new FileInfo(sourcePath); if ((f.Length / 1024) >= compressSize) { XDebug.Log.l("开始压缩,图片原始大小为:" + f.Length / 1000 + "Kb"); } int qualityI = 50; WWW www = new WWW("file:///" + sourcePath); yield return www; if (www.error != null) { if (action != null) action(false); //发返回失败 } else { Texture2D t2d = www.texture; byte[] b = t2d.EncodeToJPG(qualityI); XDebug.Log.l("图原始读取的字节数 " + (b.Length / 1000).ToString()); while ((b.Length / 1024) >= compressSize && qualityI>0) { qualityI -= 5; b = t2d.EncodeToJPG(qualityI); XDebug.Log.l("当前大小:" + b.Length / 1000); } XDebug.Log.l("压缩成功,当前大小:" + b.Length / 1000); File.WriteAllBytes(outPath, b); if (action != null) action(true); } www.Dispose(); } }