I have an expensive (I believe) function, that however is related purely to visuals, so I'd like to call it parallelly with the main code, to not bog it down. Info online on it is kinda super confusing at times, but I gathered that async is the way to go. Would that work?
//TODO: Make Recalculate() async
private bool meshIsRecalculating;
private bool recalculationQueued;
/// <summary> Queue recalculation of the decal mesh </summary>
public async Task<bool> RecalculateAsync()
{
if (meshIsRecalculating)
{
if(recalculationQueued) return false;
recalculationQueued = true;
return false;
}
meshIsRecalculating = true;
await Task.Run(Recalculate);
meshIsRecalculating = false;
return true;
}
/// <summary> Recalculate the decal mesh </summary>
public void Recalculate()
{
//The expensive calcs here, to cut out intersecting meshes into a final decal mesh
if (recalculationQueued)
{
recalculationQueued = false;
Recalculate();
}
}
(p.s. I'd love to just use Unity's standard decals instead, but they're incompatible with my stylized lighting shader)