11using UnityEngine ;
2- using System ;
32using System . Collections . Generic ;
43using System . Threading . Tasks ;
5- using System . Collections . Concurrent ;
6- using System . Linq ;
74
85namespace Extensions . Unity . ImageLoader
96{
107 public static partial class ImageLoader
118 {
12- private static ConcurrentDictionary < string , Future < Sprite > > loadingInProcess = new ConcurrentDictionary < string , Future < Sprite > > ( ) ;
13- private static void AddLoading ( Future < Sprite > future )
14- {
15- if ( ! loadingInProcess . TryAdd ( future . Url , future ) )
16- throw new Exception ( $ "[ImageLoader] Future[id={ future . Id } ] AddLoading: already loading\n { future . Url } ") ;
17-
18- if ( settings . debugLevel . IsActive ( DebugLevel . Log ) )
19- Debug . Log ( $ "[ImageLoader] Future[id={ future . Id } ] AddLoading: total { loadingInProcess . Count } loading tasks\n { future . Url } ") ;
20- }
21- private static void RemoveLoading ( Future < Sprite > future ) => RemoveLoading ( future . Url ) ;
22- private static void RemoveLoading ( string url )
23- {
24- if ( loadingInProcess . TryRemove ( url , out var future ) )
25- {
26- if ( settings . debugLevel . IsActive ( DebugLevel . Log ) )
27- Debug . Log ( $ "[ImageLoader] Future[id={ future . Id } ] RemoveLoading: left { loadingInProcess . Count } loading tasks\n { url } ") ;
28- }
29- else
30- {
31- if ( settings . debugLevel . IsActive ( DebugLevel . Warning ) )
32- Debug . LogWarning ( $ "[ImageLoader] Future[id={ future . Id } ] RemoveLoading: not found in loading tasks\n { url } ") ;
33- }
34- }
35-
369 /// <summary>
3710 /// Initialization of static variables, should be called from main thread at project start
3811 /// </summary>
@@ -46,20 +19,39 @@ public static void Init()
4619 /// Check if the url is loading right now
4720 /// </summary>
4821 /// <returns>Returns true if the url is loading right now</returns>
49- public static bool IsLoading ( string url ) => loadingInProcess . ContainsKey ( url ) ;
22+ public static bool IsLoadingSprite < T > ( string url ) => Future < Sprite > . IsLoading ( url ) ;
23+
24+ /// <summary>
25+ /// Check if the url is loading right now
26+ /// </summary>
27+ /// <returns>Returns true if the url is loading right now</returns>
28+ public static bool IsLoadingTexture < T > ( string url ) => Future < Texture2D > . IsLoading ( url ) ;
5029
5130 /// <summary>
5231 /// Find and return current loading Future by the url
5332 /// <param name="url">URL to the picture, web or local</param>
5433 /// </summary>
5534 /// <returns>Returns current loading Future or null if none</returns>
56- public static Future < Sprite > GetLoadingFuture ( string url ) => loadingInProcess . TryGetValue ( url , out var future ) ? future : null ;
35+ public static Future < Sprite > GetLoadingSpriteFuture < T > ( string url ) => Future < Sprite > . GetLoadingFuture ( url ) ;
36+
37+ /// <summary>
38+ /// Find and return current loading Future by the url
39+ /// <param name="url">URL to the picture, web or local</param>
40+ /// </summary>
41+ /// <returns>Returns current loading Future or null if none</returns>
42+ public static Future < Texture2D > GetLoadingTextureFuture < T > ( string url ) => Future < Texture2D > . GetLoadingFuture ( url ) ;
43+
44+ /// <summary>
45+ /// Return all current loading Futures
46+ /// </summary>
47+ /// <returns>Returns read only list of all current loading Futures</returns>
48+ public static IReadOnlyCollection < Future < Sprite > > GetLoadingSpriteFutures ( ) => Future < Sprite > . GetLoadingFutures ( ) ;
5749
5850 /// <summary>
5951 /// Return all current loading Futures
6052 /// </summary>
6153 /// <returns>Returns read only list of all current loading Futures</returns>
62- public static IReadOnlyCollection < Future < Sprite > > GetLoadingFutures ( ) => loadingInProcess . Values . ToList ( ) . AsReadOnly ( ) ;
54+ public static IReadOnlyCollection < Future < Texture2D > > GetLoadingTextureFutures ( ) => Future < Texture2D > . GetLoadingFutures ( ) ;
6355
6456 /// <summary>
6557 /// Clear cache from Memory and Disk layers for all urls
0 commit comments