PLEASE allow us to pass MANAGED types to jobs !

I mostly use jobs together with the entities package. And i often run into cases where i wanna acess managed classes from within jobs. Sometimes from within multithreaded jobs, but mostly from single threaded jobs ( IJobEntityBatch… ).

However, this is NOT possible. I can understand that the devs permit managed types in scheduled or parallel jobs… however i can NOT understand why this is the case for jobs being executed on the mainthread.

Furthermore it should be our responsibility, those restrictions feel like some sort of child security system. But most of us have enough experience to know what to do and when it makes sense to acess managed types or when it may create race conditions. We arent children anymore. Of course burst also plays a roll here, but not everyone uses burst aswell.

Of course we can pin managed types and pass their pointer to our jobs, but this is additional work and doesnt always work ( looking at you generic delegates ! ).

It would be great if there would be some sort of attribute, tag or whatever to allow jobs to contain managed types directly. Give us back our responsibility !

1 Like

I wonder how IL2Cpp would work with this.

I haven’t use jobs much, but what is the advantage of non-burst main thread job over executing the code directly?

2 Likes

You can use GCHandle for situations where access to managed objects is truly necessary.
Generally speaking it is very much not how we recommend using the job system.

6 Likes

When retrofitting Burst/Jobs into existing code I usually use something like ViewAsNativeArray extensions to have access to managed arrays without copying.

In many cases, you can also uniquely refer to UnityEngine.Objects via InstanceID, and although there’s an internal FindObjectFromInstanceID function that you can use, I’m not sure that’s entirely reliable and I can’t recommend using it, so maybe roll your own ConcurrentDictionary<int /instanceID/, UnityEngine.Object> or something…

2 Likes