From 074779f404be3c410b6f6a7e88520e2992c8ce99 Mon Sep 17 00:00:00 2001 From: "aube.lee" Date: Wed, 26 Feb 2025 22:41:24 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=EC=84=9C=EB=B2=84=20=EC=BD=9C=EB=B0=B1?= =?UTF-8?q?=20=EB=A9=94=EC=86=8C=EB=93=9C=20=EC=8B=A4=ED=96=89=EC=9D=84=20?= =?UTF-8?q?=EC=9C=84=ED=95=9C=20=EC=9C=A0=ED=8B=B8=20=ED=81=B4=EB=9E=98?= =?UTF-8?q?=EC=8A=A4=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Backend/UnityMainThreadDispatcher.cs | 55 +++++++++++++++++++ .../Backend/UnityMainThreadDispatcher.cs.meta | 11 ++++ 2 files changed, 66 insertions(+) create mode 100644 Gameton-06/Assets/Gameton/Scripts/Backend/UnityMainThreadDispatcher.cs create mode 100644 Gameton-06/Assets/Gameton/Scripts/Backend/UnityMainThreadDispatcher.cs.meta diff --git a/Gameton-06/Assets/Gameton/Scripts/Backend/UnityMainThreadDispatcher.cs b/Gameton-06/Assets/Gameton/Scripts/Backend/UnityMainThreadDispatcher.cs new file mode 100644 index 00000000..6231162a --- /dev/null +++ b/Gameton-06/Assets/Gameton/Scripts/Backend/UnityMainThreadDispatcher.cs @@ -0,0 +1,55 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +namespace TON +{ + /// + /// 메인 스레드에서 서버 콜백 함수를 실행하도록 하기 위한 유틸 클래스 + /// + public class UnityMainThreadDispatcher : MonoBehaviour + { + private static UnityMainThreadDispatcher _instance; + private readonly Queue _executionQueue = new Queue(); + + public static UnityMainThreadDispatcher Instance() + { + if (_instance == null) + { + var go = new GameObject("UnityMainThreadDispatcher"); + _instance = go.AddComponent(); + DontDestroyOnLoad(go); + } + return _instance; + } + + private void Awake() + { + if (_instance == null) + { + _instance = this; + DontDestroyOnLoad(gameObject); + } + } + + public void Enqueue(Action action) + { + lock (_executionQueue) + { + _executionQueue.Enqueue(action); + } + } + + private void Update() + { + lock (_executionQueue) + { + while (_executionQueue.Count > 0) + { + _executionQueue.Dequeue().Invoke(); + } + } + } + } +} diff --git a/Gameton-06/Assets/Gameton/Scripts/Backend/UnityMainThreadDispatcher.cs.meta b/Gameton-06/Assets/Gameton/Scripts/Backend/UnityMainThreadDispatcher.cs.meta new file mode 100644 index 00000000..2f20a378 --- /dev/null +++ b/Gameton-06/Assets/Gameton/Scripts/Backend/UnityMainThreadDispatcher.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 1334147e9d1908b4fba948ea4d88d64c +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: