diff --git a/Runtime/codebase/Web3.cs b/Runtime/codebase/Web3.cs index 6597181e..2c0ea500 100644 --- a/Runtime/codebase/Web3.cs +++ b/Runtime/codebase/Web3.cs @@ -319,17 +319,18 @@ public static async UniTask UpdateNFTs(Commitment commitment = Commitment.Confir /// /// /// If true, notify the register listeners + /// Add a delay between requests /// public static async UniTask> LoadNFTs( bool loadTexture = true, bool notifyRegisteredListeners = true, + int requestsMillisecondsDelay = 0, Commitment commitment = Commitment.Confirmed) { loadTexture = LoadNftsTextureByDefault ?? loadTexture; if(Wallet == null) return null; var tokens = (await Wallet.GetTokenAccounts(commitment))? - .ToList() - .FindAll(m => m.Account.Data.Parsed.Info.TokenAmount.AmountUlong == 1); + .ToList(); if(tokens == null) return null; // Remove tokens not owned anymore @@ -360,13 +361,19 @@ public static async UniTask UpdateNFTs(Commitment commitment = Commitment.Confir if (tokens is {Count: > 0}) { var toFetch = tokens - .Where(item => item.Account.Data.Parsed.Info.TokenAmount.AmountUlong == 1) .Where(item => nfts .All(t => t.metaplexData.data.mint!= item.Account.Data.Parsed.Info.Mint)).ToArray(); total = nfts.Count + toFetch.Length; foreach (var item in toFetch) { + if (Application.platform == RuntimePlatform.WebGLPlayer) + { + // If we are on WebGL, we need to add a min delay between requests + requestsMillisecondsDelay = Mathf.Max(requestsMillisecondsDelay, 100); + } + if (requestsMillisecondsDelay > 0) await UniTask.Delay(requestsMillisecondsDelay); + var tNft = Nft.Nft.TryGetNftData(item.Account.Data.Parsed.Info.Mint, Rpc, loadTexture: loadTexture).AsUniTask(); loadingTasks.Add(tNft); tNft.ContinueWith(nft => diff --git a/Runtime/codebase/nft/Nft.cs b/Runtime/codebase/nft/Nft.cs index 66689754..434d3ff4 100644 --- a/Runtime/codebase/nft/Nft.cs +++ b/Runtime/codebase/nft/Nft.cs @@ -100,7 +100,6 @@ public static Nft TryLoadNftFromLocal(string mint) /// /// Load the texture of the NFT /// - /// /// public async Task LoadTexture(int imageHeightAndWidth = 256) { diff --git a/Runtime/codebase/utility/FileDownloader.cs b/Runtime/codebase/utility/FileDownloader.cs index ed05416f..f6434fe4 100644 --- a/Runtime/codebase/utility/FileDownloader.cs +++ b/Runtime/codebase/utility/FileDownloader.cs @@ -1,7 +1,6 @@ using System; using System.Collections.Generic; using System.IO; -using System.Net.Http; using System.Threading; using System.Threading.Tasks; using Cysharp.Threading.Tasks; @@ -85,20 +84,13 @@ public static async Task LoadFile(string path, string optionalName = "") return await LoadTexture(path); } } - else - { -#if UNITY_WEBGL && !UNITY_EDITOR - return await LoadJsonWebRequest(path); -#else - return await LoadJson(path); -#endif - } + throw new NotImplementedException(); } private static async Task LoadTexture(string filePath, CancellationToken token = default) { using var uwr = UnityWebRequestTexture.GetTexture(filePath); - uwr.SendWebRequest(); + await uwr.SendWebRequest(); while (!uwr.isDone && !token.IsCancellationRequested) { @@ -111,14 +103,15 @@ private static async Task LoadTexture(string filePath, CancellationToken t return default; } var texture = DownloadHandlerTexture.GetContent(uwr); - Object.Destroy(((DownloadHandlerTexture) uwr.downloadHandler).texture); + DestroyTexture(((DownloadHandlerTexture)uwr.downloadHandler).texture); return (T)Convert.ChangeType(texture, typeof(T)); } - + private static async UniTask LoadGif(string path, CancellationToken token = default) { using UnityWebRequest uwr = UnityWebRequest.Get(path); - uwr.SendWebRequest(); + await uwr.SendWebRequest(); + while (!uwr.isDone && !token.IsCancellationRequested) { await Task.Yield(); @@ -174,56 +167,6 @@ private static Texture2D GetTextureFromGifByteStream(byte[] bytes) return null; } - private static async Task LoadJsonWebRequest(string path) - { - using var uwr = UnityWebRequest.Get(path); - uwr.downloadHandler = new DownloadHandlerBuffer(); - uwr.SendWebRequest(); - - while (!uwr.isDone) - { - await Task.Yield(); - } - - var json = uwr.downloadHandler.text; - if (uwr.result is UnityWebRequest.Result.ConnectionError or UnityWebRequest.Result.ProtocolError) - { - Debug.Log(uwr.error); - return default; - } - - Debug.Log(json); - try - { - var data = JsonConvert.DeserializeObject(json); - return data; - } - catch - { - return default; - } - } - - private static async Task LoadJson(string path) - { - var client = new HttpClient(); - - try - { - var response = await client.GetAsync(path); - response.EnsureSuccessStatusCode(); - var responseBody = await response.Content.ReadAsStringAsync(); - var data = Newtonsoft.Json.JsonConvert.DeserializeObject(responseBody); - client.Dispose(); - return data; - } - catch - { - client.Dispose(); - return default; - } - } - public static T LoadFileFromLocalPath(string path) { if (!File.Exists(path)) @@ -298,8 +241,21 @@ public static Texture2D Resize(Texture texture2D, int targetX, int targetY) Texture2D result = new Texture2D(targetX, targetY); result.ReadPixels(new Rect(0, 0, targetX, targetY), 0, 0); result.Apply(); + DestroyTexture(texture2D); return result; } + + private static void DestroyTexture(Texture texture) + { + if (Application.isPlaying) + { + Object.Destroy(texture); + } + else + { + Object.DestroyImmediate(texture); + } + } }