Skip to content

Commit f5430ef

Browse files
authored
Merge pull request #190 from maifeeulasad/stuck-fix
solution to make network call on main thread
2 parents 8719ac6 + 549d846 commit f5430ef

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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+
}

0 commit comments

Comments
 (0)