-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improved UI states when hosting a dedicated client + fixed thread issues
- Loading branch information
Showing
5 changed files
with
94 additions
and
39 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
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
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,47 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using UnityEngine; | ||
|
||
public class MainThreadDispatcher : MonoBehaviour | ||
{ | ||
private static MainThreadDispatcher _instance; | ||
private readonly Queue<Action> _actions = new Queue<Action>(); | ||
|
||
private void Awake() | ||
{ | ||
if (_instance == null) | ||
{ | ||
_instance = this; | ||
DontDestroyOnLoad(gameObject); | ||
} | ||
else | ||
{ | ||
Destroy(gameObject); | ||
} | ||
} | ||
|
||
private void Update() | ||
{ | ||
lock (_actions) | ||
{ | ||
while (_actions.Count > 0) | ||
{ | ||
_actions.Dequeue()(); | ||
} | ||
} | ||
} | ||
|
||
public static void RunOnMainThread(Action action) | ||
{ | ||
if (_instance == null) | ||
{ | ||
Debug.LogError("MainThreadDispatcher is not initialized!"); | ||
return; | ||
} | ||
|
||
lock (_instance._actions) | ||
{ | ||
_instance._actions.Enqueue(action); | ||
} | ||
} | ||
} |