Skip to content

A modular, thread-safe dispatcher for marshalling calls to Unity's main thread. Supports async/await, delayed execution, and coroutine-based scheduling

License

Notifications You must be signed in to change notification settings

techdigga/unity-ui-thread-dispatcher

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Unity UI Thread Dispatcher

License: MIT Unity Version

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.

✨ Features

  • 🔒 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

📦 Installation

Via Git URL (Recommended)

  1. Open Unity Package Manager (WindowPackage Manager)
  2. Click +Add package from git URL...
  3. Enter: https://github.com/yourusername/unity-ui-thread-dispatcher.git

🚀 Quick Start

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

About

A modular, thread-safe dispatcher for marshalling calls to Unity's main thread. Supports async/await, delayed execution, and coroutine-based scheduling

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages