Documentation › GetFromMainThread
GetFromMainThread
Executes an action on Unity’s main thread and blocks the calling worker until it has completed.
Instance method
Blocking
C# API
METHOD SIGNATURE
void GetFromMainThread(Action function);
Description
Runs the supplied parameterless action on Unity’s main thread and blocks the current worker until that action is complete. The method returns no value. Store required Unity data inside the action in a dedicated class field, then use that field after the call returns.
Synchronous hand-off
The current worker cannot continue until the main-thread action has finished.
Store values in fields
Assign values such as a Transform position to a class variable like Vector3 m_position.
Parameters
Name
Type
Description
function
Action
A parameterless action with no return value that is executed synchronously on Unity’s main thread.
Returns
The method returns void. Values needed by the worker must be assigned inside the Action to an accessible class field.
Code Example
EXAMPLE
using ThreadHarmonizerEngine;
using UnityEngine;
public class DistanceCalculator : MonoBehaviourMultithreaded
{
private Vector3 m_position;
private float m_distanceFromOrigin;
private void Start()
{
AddFunction(ParallelUpdate);
}
private void ParallelUpdate()
{
GetFromMainThread(() =>
{
m_position = transform.position;
});
// The worker continues after m_position has been assigned.
m_distanceFromOrigin = m_position.magnitude;
}
}
