|
1 | | -# unity-ui-thread-dispatcher |
2 | | -A modular, thread-safe dispatcher for marshalling calls to Unity's main thread. Supports async/await, delayed execution, and coroutine-based scheduling |
| 1 | +# Unity UI Thread Dispatcher |
| 2 | + |
| 3 | +[](https://opensource.org/licenses/MIT) |
| 4 | +[](https://unity3d.com/get-unity/download) |
| 5 | + |
| 6 | +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. |
| 7 | + |
| 8 | +## ✨ Features |
| 9 | + |
| 10 | +- 🔒 **Thread-Safe** - Lock-based queue ensures safe cross-thread communication |
| 11 | +- ⚡ **Multiple Dispatch Methods** - Immediate, delayed, next frame, end of frame |
| 12 | +- 🔄 **Async/Await Support** - Modern C# patterns with custom awaiter |
| 13 | +- 🧩 **Modular Architecture** - Interface-based design for testability |
| 14 | +- 🎯 **Zero Dependencies** - Pure Unity/C# implementation |
| 15 | +- 📦 **UPM Compatible** - Easy installation via Unity Package Manager |
| 16 | + |
| 17 | +## 📦 Installation |
| 18 | + |
| 19 | +### Via Git URL (Recommended) |
| 20 | + |
| 21 | +1. Open Unity Package Manager (`Window` → `Package Manager`) |
| 22 | +2. Click `+` → `Add package from git URL...` |
| 23 | +3. Enter: `https://github.com/yourusername/unity-ui-thread-dispatcher.git` |
| 24 | + |
| 25 | + |
| 26 | +## 🚀 Quick Start |
| 27 | +```csharp |
| 28 | +using ThreadDispatcher; |
| 29 | +using System.Threading.Tasks; |
| 30 | + |
| 31 | +// Simple callback from background thread |
| 32 | +Task.Run(() => { |
| 33 | + // Heavy work on background thread |
| 34 | + System.Threading.Thread.Sleep(1000); |
| 35 | + |
| 36 | + // Update Unity objects on main thread |
| 37 | + UIThreadDispatcher.RunOnMain(() => { |
| 38 | + transform.position = Vector3.zero; |
| 39 | + }); |
| 40 | +}); |
| 41 | + |
| 42 | +// Delayed execution |
| 43 | +UIThreadDispatcher.EnqueueWithDelay(() => { |
| 44 | + Debug.Log("Executed after 2 seconds"); |
| 45 | +}, 2f); |
| 46 | + |
| 47 | +// Async/await pattern |
| 48 | +await UIThreadExtensions.SwitchToMainThread(); |
| 49 | +transform.Rotate(0, 45, 0); |
0 commit comments