EnqueueOnMainThread
void EnqueueOnMainThread(Action function);
Runs a function on Unity’s main thread. This function should be used exclusively for accessing Unity’s main thread, given that most Unity functions can only be invoked from there.
Parameters
function
Function to be added. Only takes functions with no parameters and no return value.
Example
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : ThreadHarmonizerEngine.MonoBehaviourMultithreaded
{
// Start is called before the first frame update
void Start()
{
AddFunction(ParallelUpdate);
}
// ParallelUpdate is called once per frame
void ParallelUpdate()
{
// Do some heavy calculations
EnqueueOnMainThread(() =>
// Preferably something that has more meaning
gameObject.transform.position = new Vector3(0, 0, 0)
);
}
private void OnDestroy()
{
DisableMultithreading();
}
private void OnDisable()
{
DisableMultithreading();
}
private void OnEnable()
{
EnableMultithreading();
}
}