Force IJobEntity Execute On MainThread

How to force IJobEntity to execute on MainThread?

Unity 2022.3.1f1
Entities 1.0.16

The problem is that InputAimJob is executed on WorkerThread unless I add a managed component when it will be executed on MainThread. How do I force IJobEntity to the MainThread without using a managed component?

public partial struct InputAimJob : IJobEntity
{
    public void Execute(ref CannonAimValue aimValue)
    {
        var hor = Input.GetKey(KeyCode.LeftArrow) ? -1 : Input.GetKey(KeyCode.RightArrow) ? 1 : 0;
        aimValue.HorizontalAim += 1 * hor;
        
        var ver = Input.GetKey(KeyCode.UpArrow) ? 1 : Input.GetKey(KeyCode.DownArrow) ? -1 : 0;
        aimValue.VerticalAim += 1 * ver;
    }
}

public partial struct InputAimJobTest : IJobEntity
{
    public void Execute(ref CannonAimValue aimValue, EntityGameObject entityGameObject)
    {
        var hor = Input.GetKey(KeyCode.LeftArrow) ? -1 : Input.GetKey(KeyCode.RightArrow) ? 1 : 0;
        aimValue.HorizontalAim += 1 * hor;
        
        var ver = Input.GetKey(KeyCode.UpArrow) ? 1 : Input.GetKey(KeyCode.DownArrow) ? -1 : 0;
        aimValue.VerticalAim += 1 * ver;
    }
}

Unity thinks InputAimJob executes on a non-main thread, but Profiler says the opposite

You can’t use non-threadsafe UnityEngine APIs in jobs even if you run the job on the main thread. You should read input in the system directly.

2 Likes

I get it, I need RunByRefWithoutJobs, but it’s internal. No UnityAPI in Jobs for me