From 29ef638e8174a6d4907f9e95c6118db35e50377e Mon Sep 17 00:00:00 2001 From: Kulltero <33698270+Kulltero@users.noreply.github.com> Date: Mon, 7 Nov 2022 08:30:39 +0100 Subject: [PATCH 1/3] Caches Images downloaded from the web Avoids Re-downloading the same image for following instances of the same image, re-using the texture instead (Also Removing a now-useless if check, since it *shouldnt* happen and would only confuse anyone if it did, this was left from my change to prevent mipmap issues) --- CommunityEntity.UI.cs | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/CommunityEntity.UI.cs b/CommunityEntity.UI.cs index f8209a6..0747fe4 100644 --- a/CommunityEntity.UI.cs +++ b/CommunityEntity.UI.cs @@ -13,6 +13,7 @@ public partial class CommunityEntity { private static List AllUi = new List(); private static Dictionary UiDict = new Dictionary(); + private static Dictionary WebImageCache = new Dictionary(); public static void DestroyServerCreatedUI() { @@ -348,6 +349,11 @@ private void GraphicComponentCreated( UnityEngine.UI.Graphic c, JSON.Object obj private IEnumerator LoadTextureFromWWW( UnityEngine.UI.RawImage c, string p ) { + if(WebImageCache.ContainsKey(p){ + if(c != null) c.texture = WebImageCache[p]; + yield break; + } + var www = new WWW( p.Trim() ); while ( !www.isDone ) @@ -365,14 +371,10 @@ private IEnumerator LoadTextureFromWWW( UnityEngine.UI.RawImage c, string p ) Texture2D texture = new Texture2D(2, 2, TextureFormat.RGBA32, false); www.LoadImageIntoTexture(texture); - if ( c == null ) - { - Debug.Log( "Error downloading image: " + p + " (not an image)" ); - www.Dispose(); - yield break; - } - + c.texture = texture; + + WebImageCache.Add(texture); www.Dispose(); } From 65f8eb954795418b4dc5b0275c2773c6fb1c239c Mon Sep 17 00:00:00 2001 From: Kulltero <33698270+Kulltero@users.noreply.github.com> Date: Mon, 7 Nov 2022 09:21:36 +0100 Subject: [PATCH 2/3] Fix Missing closing ) slight syntax fix --- CommunityEntity.UI.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CommunityEntity.UI.cs b/CommunityEntity.UI.cs index 0747fe4..b63c7f3 100644 --- a/CommunityEntity.UI.cs +++ b/CommunityEntity.UI.cs @@ -349,7 +349,7 @@ private void GraphicComponentCreated( UnityEngine.UI.Graphic c, JSON.Object obj private IEnumerator LoadTextureFromWWW( UnityEngine.UI.RawImage c, string p ) { - if(WebImageCache.ContainsKey(p){ + if(WebImageCache.ContainsKey(p)){ if(c != null) c.texture = WebImageCache[p]; yield break; } From dc3990c721c501d63007790a38656624098872eb Mon Sep 17 00:00:00 2001 From: Kulltero <33698270+Kulltero@users.noreply.github.com> Date: Mon, 7 Nov 2022 09:48:11 +0100 Subject: [PATCH 3/3] Add Request Queue Added A Request Queue to avoid cases where multiple images load the same url if they get created before the first image is cached, instead the components get added to a queue and then applied at the end --- CommunityEntity.UI.cs | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/CommunityEntity.UI.cs b/CommunityEntity.UI.cs index b63c7f3..b858dfd 100644 --- a/CommunityEntity.UI.cs +++ b/CommunityEntity.UI.cs @@ -13,7 +13,9 @@ public partial class CommunityEntity { private static List AllUi = new List(); private static Dictionary UiDict = new Dictionary(); + private static Dictionary WebImageCache = new Dictionary(); + private static Dictionary> requestingWebImages = new Dictionary>(); public static void DestroyServerCreatedUI() { @@ -353,6 +355,13 @@ private IEnumerator LoadTextureFromWWW( UnityEngine.UI.RawImage c, string p ) if(c != null) c.texture = WebImageCache[p]; yield break; } + // add to the existing request Queue if one exists, if not create one and become the main coroutine that applies the texture at the end + if(requestingWebImages.ContainsKey(p)){ + requestingWebImages[p].Add(c); + yield break; + }else{ + requestingWebImages.Add(p, new List()); + } var www = new WWW( p.Trim() ); @@ -370,11 +379,18 @@ private IEnumerator LoadTextureFromWWW( UnityEngine.UI.RawImage c, string p ) Texture2D texture = new Texture2D(2, 2, TextureFormat.RGBA32, false); - www.LoadImageIntoTexture(texture); + + if(!WebImageCache.ContainsKey(p)) WebImageCache.Add(p, texture); + if(requestingWebImages[p].Count > 0){ + foreach(var graphic in requestingWebImages[p]){ + graphic.texture = texture; + } + } + requestingWebImages.Remove(p); c.texture = texture; - WebImageCache.Add(texture); + if(!WebImageCache.ContainsKey(p)) WebImageCache.Add(p, texture); www.Dispose(); }