Documentation › AddFunction
AddFunction
Registers a parameterless function for recurring execution on ThreadHarmonizer’s shared worker pool.
Instance method
C# API
METHOD SIGNATURE
void AddFunction(Action function);
Description
The registered function participates in the recurring parallel update workflow and is executed once per frame while multithreading is enabled for the component. Read Unity-bound values during the normal Unity callback, perform Unity-independent calculations in the worker function, and apply Unity changes on the main thread.
Recurring execution
The function remains registered until it is removed, reset, or the component’s multithreading state is disabled.
Thread-safe data flow
Avoid writing the same mutable value from multiple worker functions unless you provide explicit synchronization.
Parameters
Name
Type
Description
function
Action
A parameterless function with no return value. The function should contain Unity-independent worker code.
Code Example
EXAMPLE
using ThreadHarmonizerEngine;
using UnityEngine;
public class PlayerController : MonoBehaviourMultithreaded
{
[SerializeField] private float speed = 5f;
private Vector2 input;
private Vector3 calculatedMovement;
private void Start()
{
AddFunction(ParallelUpdate);
}
private void Update()
{
// Read Unity input on the main thread.
input = new Vector2(
Input.GetAxis("Horizontal"),
Input.GetAxis("Vertical"));
// Apply the worker result on the main thread.
transform.position += calculatedMovement * Time.deltaTime;
}
private void ParallelUpdate()
{
// Unity-independent calculation on a worker thread.
calculatedMovement = new Vector3(input.x, 0f, input.y) * speed;
}
private void OnDisable()
{
DisableMultithreading();
}
private void OnEnable()
{
EnableMultithreading();
}
}
