Skip to content

Commit

Permalink
🐛 Mark Dispatcher with DontDestroyOnLoad
Browse files Browse the repository at this point in the history
Mark Dispatcher with DontDestroyOnLoad to persiste scene transitions
  • Loading branch information
GabrielePicco committed Sep 8, 2023
1 parent 3ec4319 commit 7748e12
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions NativeWebSocket/WebSocket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@
public class WebSocketDispatcher : MonoBehaviour
{
public NativeWebSocket.IWebSocket WebSocket;


private void Awake()
{
DontDestroyOnLoad(gameObject);
}

private void Update()
{
WebSocket?.DispatchMessageQueue();
Expand Down Expand Up @@ -220,6 +225,8 @@ public ConfiguredTaskAwaitable.ConfiguredTaskAwaiter GetAwaiter()
public static class WebSocket
{

private static readonly string DispatchGameObjectName = "MagicWebSocketDispatcher123";


public static IWebSocket Create(string url, string subProtocol = null)
{
Expand All @@ -238,7 +245,8 @@ public static IWebSocket Create(string url, string subProtocol = null)
{
MainThreadUtil.Run( () =>
{
wsObj = new GameObject("WebSocketDispatcher" + new Random().Next())
if (GameObject.Find(DispatchGameObjectName) != null) return;
wsObj = new GameObject(DispatchGameObjectName)
.AddComponent<WebSocketDispatcher>();
wsObj.WebSocket = ws;
});
Expand Down

0 comments on commit 7748e12

Please sign in to comment.