Skip to content

Commit ccf1204

Browse files
committed
Add sample scripts for loading sprites and textures; update existing samples for clarity and consistency
1 parent 7d2c5a8 commit ccf1204

22 files changed

+457
-277
lines changed

Assets/_PackageRoot/Runtime/ImageLoader.Cache.Memory.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,19 +40,33 @@ private static void ClearMemoryCacheOnEnterPlayMode()
4040
/// <param name="replace">replace existed cached sprite if any</param>
4141
public static void SaveToMemoryCache(string url, Texture2D texture, bool replace = false) => FutureTexture.SaveToMemoryCache(url, texture, replace);
4242

43+
/// <summary>
44+
/// Loads directly from Memory cache if exists and allowed
45+
/// </summary>
46+
/// <param name="url">URL to the picture, web or local</param>
47+
/// <returns>Returns null if not allowed to use Memory cache or if there is no cached Texture2D</returns>
48+
public static Reference<Texture2D> LoadTextureRefFromMemoryCache(string url) => FutureTexture.LoadFromMemoryCacheRef(url);
49+
4350
/// <summary>
4451
/// Loads directly from Memory cache if exists and allowed
4552
/// </summary>
4653
/// <param name="url">URL to the picture, web or local</param>
4754
/// <returns>Returns null if not allowed to use Memory cache or if there is no cached Sprite</returns>
48-
public static Reference<Sprite> LoadFromMemoryCacheRef(string url) => FutureSprite.LoadFromMemoryCacheRef(url);
55+
public static Reference<Sprite> LoadSpriteRefFromMemoryCache(string url) => FutureSprite.LoadFromMemoryCacheRef(url);
56+
57+
/// <summary>
58+
/// Loads directly from Memory cache if exists and allowed
59+
/// </summary>
60+
/// <param name="url">URL to the picture, web or local</param>
61+
/// <returns>Returns null if not allowed to use Memory cache or if there is no cached Texture2D</returns>
62+
public static Texture2D LoadTextureFromMemoryCache(string url) => FutureTexture.LoadFromMemoryCache(url);
4963

5064
/// <summary>
5165
/// Loads directly from Memory cache if exists and allowed
5266
/// </summary>
5367
/// <param name="url">URL to the picture, web or local</param>
5468
/// <returns>Returns null if not allowed to use Memory cache or if there is no cached Sprite</returns>
55-
public static Sprite LoadFromMemoryCache(string url) => FutureSprite.LoadFromMemoryCache(url);
69+
public static Sprite LoadSpriteFromMemoryCache(string url) => FutureSprite.LoadFromMemoryCache(url);
5670

5771
/// <summary>
5872
/// Clear Memory cache for the given url
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "Extensions.Unity.ImageLoader.Samples",
3+
"references": [
4+
"UniTask",
5+
"Extensions.Unity.ImageLoader"
6+
],
7+
"includePlatforms": [],
8+
"excludePlatforms": [],
9+
"allowUnsafeCode": false,
10+
"overrideReferences": false,
11+
"precompiledReferences": [],
12+
"autoReferenced": true,
13+
"defineConstraints": [],
14+
"versionDefines": [],
15+
"noEngineReferences": false
16+
}

Assets/_PackageRoot/Samples/Extensions.Unity.ImageLoader.Samples.asmdef.meta

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/_PackageRoot/Samples/SampleAwaitAndForget.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ async void Start()
1515
// Load image, set image and wait
1616
await ImageLoader.LoadSprite(imageURL).ThenSet(image);
1717

18-
// Let's skip waiting for completion.
18+
// Skip waiting for completion.
1919
// To do that we can simply remove 'await' from the start.
2020
// To avoid compilation warning need to add '.Forget()'.
2121
ImageLoader.LoadSprite(imageURL).ThenSet(image).Forget();

Assets/_PackageRoot/Samples/SampleCache.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ static void OverrideCache()
1616
ImageLoader.SaveToMemoryCache(url, sprite);
1717

1818
// Take from Memory cache for specific image if exists
19-
ImageLoader.LoadFromMemoryCache(url);
19+
ImageLoader.LoadSpriteFromMemoryCache(url);
2020
}
2121
static void DoesCacheContainImage()
2222
{
@@ -26,7 +26,7 @@ static void DoesCacheContainImage()
2626
// Check if Memory cache contains specific image
2727
ImageLoader.MemoryCacheContains(url);
2828

29-
// Check if Memory cache contains specific image
29+
// Check if Disk cache contains specific image
3030
ImageLoader.DiskCacheContains(url);
3131
}
3232
static void ClearImage()

Assets/_PackageRoot/Samples/SampleCancellation.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using Extensions.Unity.ImageLoader;
1+
using Cysharp.Threading.Tasks;
2+
using Extensions.Unity.ImageLoader;
23
using System.Threading;
34
using UnityEngine;
45
using UnityEngine.UI;
@@ -30,6 +31,15 @@ void Start()
3031
.Forget();
3132
}
3233

34+
void DestroyWithMonoBehaviour()
35+
{
36+
ImageLoader.LoadSprite(imageURL)
37+
.ThenSet(image)
38+
.CancelOnEnable(this) // cancel on OnEnable event of current MonoBehaviour
39+
.CancelOnDisable(this) // cancel on OnDisable event of current MonoBehaviour
40+
.CancelOnDestroy(this); // cancel on OnDestroy event of current MonoBehaviour
41+
}
42+
3343
void SimpleCancellation()
3444
{
3545
var future = ImageLoader.LoadSprite(imageURL).ThenSet(image);

Assets/_PackageRoot/Samples/SampleErrorHandle.cs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,20 @@
44

55
public class SampleErrorHandle : MonoBehaviour
66
{
7-
[SerializeField] string imageURL;
8-
[SerializeField] Image image;
7+
[SerializeField] string imageURL; // URL of the image to be loaded
8+
[SerializeField] Image image; // UI Image component to display the loaded sprite
99

1010
void Start()
1111
{
12-
ImageLoader.LoadSprite(imageURL) // load sprite
13-
.ThenSet(image) // if success set sprite into image
14-
.Failed(exception => Debug.LogException(exception)) // if fail print exception
15-
.Forget();
12+
ImageLoader.LoadSprite(imageURL) // Attempt to load a sprite
13+
.ThenSet(image) // If successful, set the sprite to the Image component
14+
.Failed(exception => Debug.LogException(exception)) // If an error occurs, log the exception
15+
.Forget(); // Forget the task to avoid compilation warning
1616

17-
ImageLoader.LoadSprite(imageURL) // load sprite
18-
.ThenSet(image) // if success set sprite into image
19-
.Then(sprite => image.gameObject.SetActive(true)) // if success activate gameObject
20-
.Failed(exception => image.gameObject.SetActive(false)) // if fail deactivate gameObject
21-
.Forget();
17+
ImageLoader.LoadSprite(imageURL) // Attempt to load a sprite
18+
.ThenSet(image) // If successful, set the sprite to the Image component
19+
.Then(sprite => image.gameObject.SetActive(true)) // If successful, activate the GameObject
20+
.Failed(exception => image.gameObject.SetActive(false)) // If an error occurs, deactivate the GameObject
21+
.Forget(); // Forget the task to avoid compilation warning
2222
}
2323
}

Assets/_PackageRoot/Samples/SampleImageLoading.cs

Lines changed: 0 additions & 25 deletions
This file was deleted.

Assets/_PackageRoot/Samples/SampleLifecycle.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44

55
public class SampleLifecycle : MonoBehaviour
66
{
7-
[SerializeField] string imageURL;
8-
[SerializeField] Image image;
7+
[SerializeField] string imageURL; // URL of the image to be loaded
8+
[SerializeField] Image image; // UI Image component to display the loaded sprite
99

1010
void Start()
1111
{
12-
ImageLoader.LoadSprite(imageURL) // load sprite
12+
ImageLoader.LoadSprite(imageURL) // loading process started
1313
// ┌──────────────────────────┬────────────────────────────────────────────────────────────────────────┐
1414
// │ Loading lifecycle events │ │
1515
// └──────────────────────────┘ │
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using Extensions.Unity.ImageLoader;
2+
using UnityEngine;
3+
using UnityEngine.UI;
4+
5+
public class SampleLoadSpriteThenSetImage : MonoBehaviour
6+
{
7+
[SerializeField] string imageURL; // URL of the image to be loaded
8+
[SerializeField] Image image; // UI Image component to display the loaded sprite
9+
10+
async void Start()
11+
{
12+
// Load a sprite from the web and cache it for faster loading next time
13+
image.sprite = await ImageLoader.LoadSprite(imageURL);
14+
15+
// Load a sprite from the web and set it directly to the Image component
16+
await ImageLoader.LoadSprite(imageURL).ThenSet(image);
17+
}
18+
}

0 commit comments

Comments
 (0)