56 lines
1.4 KiB
C#
56 lines
1.4 KiB
C#
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();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|