using System; using System.Collections.Concurrent; using UnityEngine; namespace BrewMonster.Scripts.ChatUI { /// /// Responsible for switching chat messages from background threads to Unity main thread. /// Only handles chat related actions. /// public class ChatThreadDispatcher : MonoSingleton { private static readonly ConcurrentQueue _queue = new ConcurrentQueue(); protected override void Awake() { base.Awake(); DontDestroyOnLoad(gameObject); } /// /// Called from ANY thread (network thread safe) /// public void Post(Action action) { if (action == null) return; _queue.Enqueue(action); } private void Update() { while (_queue.TryDequeue(out var action)) { try { action?.Invoke(); } catch (Exception e) { Debug.LogError($"ChatThreadDispatcher error: {e}"); } } } } }