A modular, thread-safe dispatcher for marshalling calls to Unity's main thread. Perfect for handling asynchronous operations, network callbacks, or any background thread work that needs to update Unity objects.
- 🔒 Thread-Safe - Lock-based queue ensures safe cross-thread communication
- ⚡ Multiple Dispatch Methods - Immediate, delayed, next frame, end of frame
- 🔄 Async/Await Support - Modern C# patterns with custom awaiter
- 🧩 Modular Architecture - Interface-based design for testability
- 🎯 Zero Dependencies - Pure Unity/C# implementation
- 📦 UPM Compatible - Easy installation via Unity Package Manager
- Open Unity Package Manager (
Window
→Package Manager
) - Click
+
→Add package from git URL...
- Enter:
https://github.com/yourusername/unity-ui-thread-dispatcher.git
using ThreadDispatcher;
using System.Threading.Tasks;
// Simple callback from background thread
Task.Run(() => {
// Heavy work on background thread
System.Threading.Thread.Sleep(1000);
// Update Unity objects on main thread
UIThreadDispatcher.RunOnMain(() => {
transform.position = Vector3.zero;
});
});
// Delayed execution
UIThreadDispatcher.EnqueueWithDelay(() => {
Debug.Log("Executed after 2 seconds");
}, 2f);
// Async/await pattern
await UIThreadExtensions.SwitchToMainThread();
transform.Rotate(0, 45, 0);