File tree 2 files changed +44
-0
lines changed
src/Proyecto26.RestClient/Helpers
2 files changed +44
-0
lines changed Original file line number Diff line number Diff line change @@ -55,6 +55,7 @@ RestClient.GetArray<Post>(api + "/posts").Then(response => {
55
55
- Handle HTTP exceptions in a better way
56
56
- Retry HTTP requests easily
57
57
- Open Source 🦄
58
+ - Utility to work during scene transition
58
59
59
60
## Supported platforms 📱 🖥
60
61
The [ UnityWebRequest] ( https://docs.unity3d.com/Manual/UnityWebRequest.html ) system supports most Unity platforms:
@@ -112,6 +113,13 @@ RestClient.Head("https://jsonplaceholder.typicode.com/posts").Then(response => {
112
113
});
113
114
```
114
115
116
+ ## Handling during scene transition
117
+ ``` csharp
118
+ ExecuteOnMainThread .RunOnMainThread .Enqueue (() => {
119
+ // Any API call using RestClient
120
+ });
121
+ ```
122
+
115
123
### Generic Request Method
116
124
And we have a generic method to create any type of request:
117
125
``` csharp
Original file line number Diff line number Diff line change
1
+ using System ;
2
+ using System . Collections . Generic ;
3
+ using System . Collections . Concurrent ;
4
+ using UnityEngine ;
5
+
6
+ namespace Proyecto26 . Helper {
7
+
8
+ /// <summary>
9
+ /// Original solution from: https://stackoverflow.com/a/53818416/10305444
10
+ /// Should also read: https://gamedev.stackexchange.com/a/195298/126092
11
+ /// Usage:
12
+ /// ```
13
+ /// ExecuteOnMainThread.RunOnMainThread.Enqueue(() => {
14
+ /// Code here will be called in the main thread...
15
+ /// });
16
+ /// ```
17
+ /// </summary>
18
+ public class ExecuteOnMainThread : MonoBehaviour {
19
+
20
+ /// <summary>
21
+ /// Store all instance of Action's and try to invoke them
22
+ /// </summary>
23
+ public static readonly ConcurrentQueue < Action > RunOnMainThread = new ConcurrentQueue < Action > ( ) ;
24
+
25
+ /// <summary>
26
+ /// Unity's Update method
27
+ /// </summary>
28
+ void Update ( ) {
29
+ if ( ! RunOnMainThread . IsEmpty ) {
30
+ while ( RunOnMainThread . TryDequeue ( out var action ) ) {
31
+ action ? . Invoke ( ) ;
32
+ }
33
+ }
34
+ }
35
+ }
36
+ }
You can’t perform that action at this time.
0 commit comments