Set [ReadOnly] attribute at runtime?

Hi - I need to set (and sometimes not set) the [ReadOnly] attribute for an IJob’s NativeHashMap field at runtime. Is this possible? Thanks!

To my knowledge this is not possible or rather intended. Like most things, it’s probably possible to hack a solution with reflections, but for most intents and purposes, doing something with reflections means you are approaching it wrong. Not saying that there are no legitimate uses for reflections tho. According to a stackoverflow post i found, you can use reflections to change readonly values. Whether or not you can change the readonly modifier itself is another question, and [ReadOnly] is something else entirely too.

However, in my experience [ReadOnly] does not make that huge a difference. So if something can either be readonly or not, then as far as i’m concerned, it’s not. And if you think about it, that makes sense. If you have a clear distinction between two jobs, one where it’s [ReadOnly] and one where it’s not, then you may as well create two jobs for this that call the same function as content, so you dont have to copypaste the whole code.

What’s your usecase for why you need this?

Thanks - My IJob is generating a large voxel-based mesh and uses a persistent NativeHashMap as a vertex cache (read&write), because I have a lot of duplicate vertices which are expensive to calculate. At some point, bits and pieces break off the mesh. The code for generating the fragment meshes is identical to the IJob above. Only now I want to run multiple IJobs at once, each one only reading from the cache.

Everything is possible, but I’d strongly recommend against it :