From 7fb2164505244489beb759e6a30e6803d56108e2 Mon Sep 17 00:00:00 2001 From: Maifee Ul Asad Date: Tue, 19 Oct 2021 20:33:36 +0600 Subject: [PATCH 1/2] solution to call on main thread --- .../Helpers/ExecuteOnMainThread.cs | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 src/Proyecto26.RestClient/Helpers/ExecuteOnMainThread.cs diff --git a/src/Proyecto26.RestClient/Helpers/ExecuteOnMainThread.cs b/src/Proyecto26.RestClient/Helpers/ExecuteOnMainThread.cs new file mode 100644 index 0000000..df1f50d --- /dev/null +++ b/src/Proyecto26.RestClient/Helpers/ExecuteOnMainThread.cs @@ -0,0 +1,36 @@ +using System; +using System.Collections.Generic; +using System.Collections.Concurrent; +using UnityEngine; + +namespace Proyecto26.Helper { + + /// + /// Original solution from: https://stackoverflow.com/a/53818416/10305444 + /// Should also read: https://gamedev.stackexchange.com/a/195298/126092 + /// Usage: + /// ``` + /// ExecuteOnMainThread.RunOnMainThread.Enqueue(() => { + /// Code here will be called in the main thread... + /// }); + /// ``` + /// + public class ExecuteOnMainThread: MonoBehaviour { + + /// + /// Store all instance of Action's and try to invoke them + /// + public static readonly ConcurrentQueue < Action > RunOnMainThread = new ConcurrentQueue < Action > (); + + /// + /// Unity's Update method + /// + void Update() { + if (!RunOnMainThread.IsEmpty) { + while (RunOnMainThread.TryDequeue(out var action)) { + action?.Invoke(); + } + } + } + } +} \ No newline at end of file From f3228a6323a4bf1c2d40c7418f0afcb2b198f7b4 Mon Sep 17 00:00:00 2001 From: Maifee Ul Asad Date: Tue, 19 Oct 2021 20:46:38 +0600 Subject: [PATCH 2/2] added some readme --- README.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/README.md b/README.md index 3abf78f..a1f18f4 100644 --- a/README.md +++ b/README.md @@ -55,6 +55,7 @@ RestClient.GetArray(api + "/posts").Then(response => { - Handle HTTP exceptions in a better way - Retry HTTP requests easily - Open Source 🦄 +- Utility to work during scene transition ## Supported platforms 📱 🖥 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 => { }); ``` +## Handling during scene transition +```csharp +ExecuteOnMainThread.RunOnMainThread.Enqueue(() => { + //Any API call using RestClient +}); +``` + ### Generic Request Method And we have a generic method to create any type of request: ```csharp