Skip to content

Commit 9016525

Browse files
authored
Merge pull request #16 from IvanMurzak/feature/future
Feature/future
2 parents 8cf41c6 + 5a8a119 commit 9016525

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+1778
-314
lines changed

.vscode/settings.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{
1+
{
22
"files.exclude":
33
{
44
"**/.DS_Store":true,
@@ -51,5 +51,9 @@
5151
"ProjectSettings/":true,
5252
"temp/":true,
5353
"Temp/":true
54-
}
54+
},
55+
"dotnet.preferCSharpExtension": true,
56+
"cSpell.words": [
57+
"ARGB"
58+
]
5559
}

.vscode/tasks.json

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
{
2+
"version": "2.0.0",
3+
"tasks": [
4+
{
5+
"label": "build",
6+
"command": "dotnet",
7+
"type": "process",
8+
"args": [
9+
"build",
10+
"${workspaceFolder}/Unity-ImageLoader.sln",
11+
"/property:GenerateFullPaths=true",
12+
"/consoleloggerparameters:NoSummary;ForceNoAlign"
13+
],
14+
"problemMatcher": "$msCompile"
15+
},
16+
{
17+
"label": "publish",
18+
"command": "dotnet",
19+
"type": "process",
20+
"args": [
21+
"publish",
22+
"${workspaceFolder}/Unity-ImageLoader.sln",
23+
"/property:GenerateFullPaths=true",
24+
"/consoleloggerparameters:NoSummary;ForceNoAlign"
25+
],
26+
"problemMatcher": "$msCompile"
27+
},
28+
{
29+
"label": "watch",
30+
"command": "dotnet",
31+
"type": "process",
32+
"args": [
33+
"watch",
34+
"run",
35+
"--project",
36+
"${workspaceFolder}/Unity-ImageLoader.sln"
37+
],
38+
"problemMatcher": "$msCompile"
39+
}
40+
]
41+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using Extensions.Unity.ImageLoader;
2+
using UnityEngine;
3+
using UnityEngine.UI;
4+
5+
public class SampleAwaitAndForget : MonoBehaviour
6+
{
7+
[SerializeField] string imageURL;
8+
[SerializeField] Image image;
9+
10+
async void Start()
11+
{
12+
// Load image and wait
13+
await ImageLoader.LoadSprite(imageURL);
14+
15+
// Load image, set image and wait
16+
await ImageLoader.LoadSprite(imageURL).ThenSet(image);
17+
18+
// Let's skip waiting for completion.
19+
// To do that we can simply remove 'await' from the start.
20+
// To avoid compilation warning need to add '.Forget()'.
21+
ImageLoader.LoadSprite(imageURL).ThenSet(image).Forget();
22+
}
23+
}

Assets/Samples/SampleAwaitAndForget.cs.meta

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

Assets/Samples/CacheSample.cs renamed to Assets/Samples/SampleCache.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using Extensions.Unity.ImageLoader;
22
using UnityEngine;
33

4-
static class CacheSample
4+
static class SampleCache
55
{
66
static string url = "";
77
static Sprite sprite;
File renamed without changes.
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
using Extensions.Unity.ImageLoader;
2+
using System.Threading;
3+
using UnityEngine;
4+
using UnityEngine.UI;
5+
6+
public class SampleCancellation : MonoBehaviour
7+
{
8+
[SerializeField] string imageURL;
9+
[SerializeField] Image image;
10+
11+
void Start()
12+
{
13+
ImageLoader.LoadSprite(imageURL) // load sprite
14+
.ThenSet(image) // if success set sprite into image
15+
.CancelOnDestroy(this) // cancel OnDestroy event of current gameObject
16+
.Forget();
17+
18+
ImageLoader.LoadSprite(imageURL) // load sprite
19+
.ThenSet(image) // if success set sprite into image
20+
.Failed(exception => Debug.LogException(exception)) // if fail print exception
21+
.CancelOnDestroy(this) // cancel OnDestroy event of current gameObject
22+
.Forget();
23+
24+
ImageLoader.LoadSprite(imageURL) // load sprite
25+
.ThenSet(image) // if success set sprite into image
26+
.Then(sprite => image.gameObject.SetActive(true)) // if success activate gameObject
27+
.Failed(exception => image.gameObject.SetActive(false)) // if fail deactivate gameObject
28+
.Cancelled(() => Debug.Log("ImageLoading cancelled")) // if cancelled
29+
.CancelOnDisable(this) // cancel OnDisable event of current gameObject
30+
.Forget();
31+
}
32+
33+
void SimpleCancellation()
34+
{
35+
var future = ImageLoader.LoadSprite(imageURL).ThenSet(image);
36+
future.Cancel();
37+
}
38+
39+
void CancellationTokenSample1()
40+
{
41+
var cancellationTokenSource = new CancellationTokenSource();
42+
43+
// loading with attached cancellation token
44+
ImageLoader.LoadSprite(imageURL, cancellationToken: cancellationTokenSource.Token)
45+
.ThenSet(image)
46+
.Forget();
47+
48+
cancellationTokenSource.Cancel(); // canceling
49+
}
50+
51+
void CancellationTokenSample2()
52+
{
53+
var cancellationTokenSource = new CancellationTokenSource();
54+
55+
ImageLoader.LoadSprite(imageURL)
56+
.ThenSet(image)
57+
.Register(cancellationTokenSource.Token) // registering cancellation token
58+
.Forget();
59+
60+
cancellationTokenSource.Cancel(); // canceling
61+
}
62+
63+
void DisposeSample()
64+
{
65+
using (var future = ImageLoader.LoadSprite(imageURL).ThenSet(image))
66+
{
67+
// future would be canceled and disposed outside of the brackets
68+
}
69+
}
70+
}

Assets/Samples/SampleCancellation.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using Extensions.Unity.ImageLoader;
2+
using UnityEngine;
3+
using UnityEngine.UI;
4+
5+
public class SampleErrorHandle : MonoBehaviour
6+
{
7+
[SerializeField] string imageURL;
8+
[SerializeField] Image image;
9+
10+
void Start()
11+
{
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();
16+
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();
22+
}
23+
}

Assets/Samples/SampleErrorHandle.cs.meta

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

0 commit comments

Comments
 (0)