public void ConverToJPG(TexConverInfo texConverInfo) { string filePath = texConverInfo.filePath; byte[] pngBytes = File.ReadAllBytes(filePath); if (!CheckIsPNG(pngBytes)) { return; } int width = BitConverter.ToUInt16(pngBytes, 16); int hegith = BitConverter.ToUInt16(pngBytes, 20); Texture2D pngTexture2D = new Texture2D(width, hegith); pngTexture2D.LoadImage(pngBytes); byte[] jpgBytes = pngTexture2D.EncodeToJPG(75); string jpgFilePath = Path.Combine(exportDir + "/custom", texConverInfo.bundleName); File.WriteAllBytes(jpgFilePath, jpgBytes); string json = JsonMapper.ToJson(texConverInfo); string jsonFileName = Path.GetFileName(jpgFilePath + ".json"); File.WriteAllText(Path.Combine(GetExportPath(), jsonFileName), json); }
public void ConverAllJPG() { Directory.CreateDirectory(GetExportPath()); int allCount = allTexConverInfo.Count; while (allTexConverInfo.Count != 0) { TexConverInfo texConverInfo = allTexConverInfo.Dequeue(); try { int finishCount = allCount - allTexConverInfo.Count; if (_showProgressBar) { EditorUtility.DisplayProgressBar("正在转换", string.Format("转换{0}/{1}", finishCount, allCount), (float)finishCount / allCount); } ConverToJPG(texConverInfo); } catch (Exception ex) { UnityEngine.Debug.LogError(texConverInfo.ToString() + "\n" + ex.ToString()); } } if (_showProgressBar) { EditorUtility.ClearProgressBar(); } }
private bool CheckTexNeedUpdate(string filePath, Crc32 crc32, out uint crc) { byte[] fileBytes = File.ReadAllBytes(filePath); crc = CRC32Hashing.HashBytes(fileBytes); crc32.Update(fileBytes); string jsonFilePath = Path.Combine(GetExportPath(), Path.GetFileNameWithoutExtension(filePath) + ".json"); if (File.Exists(jsonFilePath)) { TexConverInfo texConverInfo = JsonMapper.ToObject <TexConverInfo>(File.ReadAllText(jsonFilePath)); if (crc == texConverInfo.sourceFileCRC) { return(false); } } return(true); }