feat: 서버 콜백 메소드 실행을 위한 유틸 클래스 추가

This commit is contained in:
aube.lee
2025-02-26 22:41:24 +09:00
parent 3613bb3f70
commit 074779f404
2 changed files with 66 additions and 0 deletions

View File

@@ -0,0 +1,55 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace TON
{
/// <summary>
/// 메인 스레드에서 서버 콜백 함수를 실행하도록 하기 위한 유틸 클래스
/// </summary>
public class UnityMainThreadDispatcher : MonoBehaviour
{
private static UnityMainThreadDispatcher _instance;
private readonly Queue<Action> _executionQueue = new Queue<Action>();
public static UnityMainThreadDispatcher Instance()
{
if (_instance == null)
{
var go = new GameObject("UnityMainThreadDispatcher");
_instance = go.AddComponent<UnityMainThreadDispatcher>();
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();
}
}
}
}
}