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();
}
}
}
}
}