压缩图片bug解决

This commit is contained in:
Caiyanpeng committed 2021-11-22 09:51:17 +08:00
1 parent 3083224ee6
commit 9570334e25
5 files changed
+25 -6

No files matched your search

+20 -1
View File
@@ -432,10 +432,29 @@ namespace Assets.Scripts
}
/*获取最顶层对象*/
/*Texture相关*/
public static Texture2D DeCompress(this Texture2D source)
{
RenderTexture renderTex = RenderTexture.GetTemporary(
source.width,
source.height,
0,
RenderTextureFormat.Default,
RenderTextureReadWrite.Linear);
Graphics.Blit(source, renderTex);
RenderTexture previous = RenderTexture.active;
RenderTexture.active = renderTex;
Texture2D readableText = new Texture2D(source.width, source.height);
readableText.ReadPixels(new Rect(0, 0, renderTex.width, renderTex.height), 0, 0);
readableText.Apply();
RenderTexture.active = previous;
RenderTexture.ReleaseTemporary(renderTex);
return readableText;
}
public static void WriteTextureToPlayerPrefs(string tag, Texture2D tex)
{
// if texture is png otherwise you can use tex.EncodeToJPG().
byte[] texByte = tex.EncodeToPNG();
byte[] texByte = tex.DeCompress().EncodeToJPG();
// convert byte array to base64 string
string base64Tex = System.Convert.ToBase64String(texByte);