diff --git a/CommunityEntity.UI.cs b/CommunityEntity.UI.cs index c8154a6..ee5df9a 100644 --- a/CommunityEntity.UI.cs +++ b/CommunityEntity.UI.cs @@ -14,6 +14,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() { foreach ( var go in AllUi ) @@ -24,6 +27,7 @@ public static void DestroyServerCreatedUI() AllUi.Clear(); UiDict.Clear(); + WebImageCache.Clear(); requestingTextureImages.Clear(); UnloadTextureCache(); } @@ -446,6 +450,18 @@ 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; + } + // 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() ); while ( !www.isDone ) @@ -463,14 +479,18 @@ 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; + + 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; + + if(!WebImageCache.ContainsKey(p)) WebImageCache.Add(p, texture); www.Dispose(); }