Class UnityMainThread
Unity MonoBehaviour singleton for managing cross-thread operations and main thread dispatching.
Provides thread-safe methods to execute actions and functions on Unity's main thread from background threads,
with automatic bootstrap initialization and concurrent queue-based operation management.
Namespace: Serenity.Global.Infrastructure.Utils
Assembly: Serenity.UnityGlobal.Infrastructure.dll
Syntax
public class UnityMainThread : MonoBehaviour
Constructors
UnityMainThread()
Declaration
public UnityMainThread()
Methods
Initialize(ILogService)
Initializes the UnityMainThread with a log service for exception logging.
Declaration
public static void Initialize(ILogService logService)
Parameters
| Type | Name | Description |
|---|---|---|
| ILogService | logService | Service for logging exceptions during action execution |
InvokeAsync(Action)
Asynchronously invokes an action on the main thread and returns a Task for completion.
Declaration
public static Task InvokeAsync(Action action)
Parameters
| Type | Name | Description |
|---|---|---|
| Action | action | Action to execute on the main thread |
Returns
| Type | Description |
|---|---|
| Task | Task that completes when the action has been executed |
InvokeAsync<T>(Func<T>)
Asynchronously invokes a function on the main thread and returns a Task with the result.
Declaration
public static Task<T> InvokeAsync<T>(Func<T> func)
Parameters
| Type | Name | Description |
|---|---|---|
| Func<T> | func | Function to execute on the main thread |
Returns
| Type | Description |
|---|---|
| Task<T> | Task containing the function's result |
Type Parameters
| Name | Description |
|---|---|
| T | Return type of the function |
Post(Action)
Posts an action to be executed on the main thread.
Thread-safe method that queues the action for execution during the next Update cycle.
Declaration
public static void Post(Action action)
Parameters
| Type | Name | Description |
|---|---|---|
| Action | action | Action to execute on the main thread |