I am trying to test job completion time within a system.
using Unity.Entities;
using UnityEngine;
[DisableAutoCreation]
public class SubSystemOne : SystemBase
{
float timer = 0f;
protected override void OnCreate()
{
EntityManager.CreateEntity(ComponentType.ReadWrite<TestValueOne>());
}
protected override void OnUpdate()
{
Entities.ForEach((ref TestValueOne one) => {
for (int i = 0; i < 10000; i++)
{
//Throws an error if I schedule
timer += Time.DeltaTime;
}
}).Schedule();
Debug.Log(timer);
World.GetExistingSystem<SimulationSystemGroup>()
.RemoveSystemFromUpdateList(World.GetExistingSystem<SubSystemOne>());
}
}
public struct TestValueOne : IComponentData
{
public float Value;
}
This works fine if I just run the job. However, if I try to schedule it, I get an error. The error is because I am trying to access a reference value inside the scheduled job, which is understandable…
I have tried querying for the WorldTime component that exists by default in the World, but this does not seem to work…
I am wondering how best to proceed with completing a test like this. I am trying to set up multiple scheduled jobs to see their results. Maybe there is a better way to approach this kind of testing?
Regardless of the fact that Time.deltaTime can only be accessed from the main thread, it makes absolutely no sense to do so unless you need a recent value for calculations on each object. The way you use it in your example code makes no sense. DeltaTime is the time between two frames on the main thread. You accumulate that time 10000 times, every object. What do you think this value would tell you in the end?
I haven’t used ECS or the Job system yet, however if you want to measure time you probably should simply use the normal System.DateTime class to get the realtime. To see how long a certain process takes, you should just get the time when it’s started and once again when it’s finished. The difference between the two tells you how long it took. Though I’m not sure if that makes any sense with the Job system.