Closed michaelsakharov closed 9 months ago
Forgot c# has a lot of Thread related helper classes, instead of building a Job System it makes more sense to just use those.
What about using System.Threading.Tasks
This is how I utilized Tasks in Unity for multi threading voxel terrain
private IEnumerator RunMeshTask(byte[] voxels, Action<Mesh> callback) {
var vertices = new();
var triangles = new();
var task = Task.Factory.StartNew(delegate {
// compute mesh
});
// wait until task is completed
yield return new WaitUntil(() => {
return task.IsCompleted;
});
var mesh = new Mesh();
mesh.vertices = vertices;
mesh.triangles = triangles;
callback(mesh);
}
// usage
StartCoroutine(RunMeshTask(voxels, (_mesh) => {
// this callback gets called once the task has finished and gives a voxel mesh
meshFilter.mesh = _mesh;
}));
That's what I meant by c# has existing solutions :P
A Efficient Multi-threaded Job System
Will be handy to have especially to offload things like Asset Importing onto.