Expand description
Main-thread dispatch queue.
Unity’s API may only be called from its main thread. Code that runs on
another thread (an egui overlay, a background task) can use queue to
fire-and-forget a closure, or call to run a closure and block until
the result is ready.
The queue is drained once per frame via super::on_update, and can
also be drained explicitly with drain_pending from any code that
holds the main thread (e.g. the egui bridge’s wait loop).
A waker (set_waker) lets the dispatch module notify sleeping main-
thread code (like a condvar wait) that work has been queued, so it can
wake up and call drain_pending without polling.
use hao_il2cpp_unity::{dispatch, screen};
std::thread::spawn(|| {
// Runs on Unity's main thread on the next frame; this thread waits.
// (The runtime's `Error` isn't `Send`, so convert it inside the closure.)
if let Ok(size) = dispatch::call(|| screen::size().ok()) {
println!("screen is {size:?}");
}
// Fire and forget.
let _ = dispatch::queue(|| {
let _ = hao_il2cpp_unity::log("hello from a background thread");
});
});Functions§
- call
- Runs
fon Unity’s main thread and blocks the caller until the result is ready. - drain_
pending - Runs all pending jobs on the current thread, then returns. Safe to call from any thread, but only useful on the main thread — the jobs expect to run there.
- queue
- Queues
fto run on Unity’s main thread on the next frame. Returns immediately without waiting forfto execute. - set_
waker - Installs a waker that is called (from the queuing thread) every time a
new job is pushed. Use this to wake a condvar or similar so the main
thread can call
drain_pendingpromptly instead of polling.