-
-
Notifications
You must be signed in to change notification settings - Fork 174
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #190 from maifeeulasad/stuck-fix
solution to make network call on main thread
- Loading branch information
Showing
2 changed files
with
44 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Collections.Concurrent; | ||
using UnityEngine; | ||
|
||
namespace Proyecto26.Helper { | ||
|
||
/// <summary> | ||
/// 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... | ||
/// }); | ||
/// ``` | ||
/// </summary> | ||
public class ExecuteOnMainThread: MonoBehaviour { | ||
|
||
/// <summary> | ||
/// Store all instance of Action's and try to invoke them | ||
/// </summary> | ||
public static readonly ConcurrentQueue < Action > RunOnMainThread = new ConcurrentQueue < Action > (); | ||
|
||
/// <summary> | ||
/// Unity's Update method | ||
/// </summary> | ||
void Update() { | ||
if (!RunOnMainThread.IsEmpty) { | ||
while (RunOnMainThread.TryDequeue(out var action)) { | ||
action?.Invoke(); | ||
} | ||
} | ||
} | ||
} | ||
} |