I found this:
and think I was somewhat able to follow it, but I get various errors trying to make it work such as: InvalidOperationException: The previously scheduled job TestAsync2System:<>c__DisplayClass_schedule_LambdaJob0 reads from the NativeArray <>c__DisplayClass_schedule_LambdaJob0.safety. You must call JobHandle.Complete() on the job TestAsync2System:<>c__DisplayClass_schedule_LambdaJob0, before you can deallocate the NativeArray safely.
and warnings like: Internal: JobTempAlloc has allocations that are more than 4 frames old - this is not allowed and likely a leak
Am I doing something wrong? I tried it without the logs and timing and saw same errors, so those aren’t causing it. It does seem to be running as expected, but all the warnings and errors are concerning.
using Unity.Entities;
using Unity.Jobs;
public class TestAsync2System : JobComponentSystem {
int count = 0;
JobHandle jobHandle;
protected override void OnCreate() {
RequireSingletonForUpdate<AsyncTag>();
}
protected override JobHandle OnUpdate(JobHandle inputDependencies) {
UnityEngine.Debug.Log("OnUpdate");
if (count == 0) {
schedule(inputDependencies);
UnityEngine.Debug.Log("inc count: "+count);
count++;
} else {
if (jobHandle.IsCompleted) {
UnityEngine.Debug.Log("completed: "+count);
jobHandle.Complete();
count = 0;
}
}
return inputDependencies;
}
private void schedule(JobHandle inputDependencies) {
jobHandle = Entities.WithAll<AsyncTag>().WithoutBurst().ForEach((Entity e) => {
System.Diagnostics.Stopwatch s = System.Diagnostics.Stopwatch.StartNew();
UnityEngine.Debug.Log("starting: " + s.ElapsedMilliseconds);
System.Threading.Thread.Sleep(1000);
UnityEngine.Debug.Log("finished: " + s.ElapsedMilliseconds);
}).Schedule(inputDependencies);
}
}