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: