diff --git a/Assets/Script/Utils/SpriteLoader.cs b/Assets/Script/Utils/SpriteLoader.cs index 5ded7c87..277380cb 100644 --- a/Assets/Script/Utils/SpriteLoader.cs +++ b/Assets/Script/Utils/SpriteLoader.cs @@ -1,4 +1,7 @@ -using System; +using SkiaSharp; +using SkiaSharp.Unity; +using System; +using System.Drawing; using System.IO; using System.Net; using System.Net.Http; @@ -29,8 +32,7 @@ public static async Task LoadAsync(string path, CancellationToken ct = d return Sprite.Create(new Texture2D(0, 0), new Rect(0, 0, 0, 0), new Vector2(0.5f, 0.5f)); var bytes = await File.ReadAllBytesAsync(path, ct); ct.ThrowIfCancellationRequested(); - var texture = new Texture2D(0, 0); - texture.LoadImage(bytes); + var texture = await ImageDecodeAsync(bytes); return Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f)); } @@ -39,7 +41,7 @@ public static async Task LoadAsync(Uri uri, CancellationToken ct = defau Directory.CreateDirectory(MajEnv.CachePath); var b64 = Convert.ToBase64String(Encoding.UTF8.GetBytes(uri.OriginalString)); var cachefile = Path.Combine(MajEnv.CachePath, b64); - byte[] bytes; + byte[] bytes = Array.Empty(); if (!File.Exists(cachefile)) { var client = new HttpClient(new HttpClientHandler() { Proxy = WebRequest.GetSystemWebProxy(), UseProxy = true }); @@ -52,8 +54,7 @@ public static async Task LoadAsync(Uri uri, CancellationToken ct = defau bytes = await File.ReadAllBytesAsync(cachefile, ct); } ct.ThrowIfCancellationRequested(); - var texture = new Texture2D(0, 0); - texture.LoadImage(bytes); + var texture = await ImageDecodeAsync(bytes); return Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f)); } @@ -67,5 +68,13 @@ public static Sprite Load(string path, Vector4 border) return Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f), 100, 1, SpriteMeshType.FullRect, border); } + async static Task ImageDecodeAsync(byte[] data) + { + using var bitmap = await Task.Run(() => + { + return SKBitmap.Decode(data); + }); + return bitmap.ToTexture2D(); + } } } \ No newline at end of file