Is the usage of the 'StructLayout' Attribute good for partial jobs

Hi,

I’ve been encountering a warning (SPDC0282) in the editor when passing values to jobs by using the job system.

After some digging, I found documentation regarding the StructLayoutAttribute, which allows a struct’s physical layout in memory. I managed to solve this error by adding the tag above the job.

[StructLayout (LayoutKind.Auto)]
public partial struct SomeJob : IJobEntity
{
public int someInt;

void Execute(ref someDataComponent data){}
}

Is this considered a good solution or would this eventually conflict with the ECS Job System?

Thanks in advance!

Auto doesn’t work in Burst.

(0,0): Burst error BC1045: Struct `...` with auto layout is not supported

The default is sequential. It should be laid out from largest to smallest data type to get the smallest struct size.
If you have something like int, byte, int the size will be 12 bytes and not 9 due to padding. The correct layout is int, int, byte.
For that to work you don’t have to specify any StructLayout.

The more interesting StructLayout is Explicit where you set your own FieldOffsets and can even let fields overlap each other. For example a field offset of 0 on 2 fields that are float and int lets you access both. Of course there will be problems when you write a float and then read an int but this kind of layout can be highly useful.

Same goes for micro-managing structs where you want to make visible to others that the layout is very specific and so not someone else comes in and puts another data type in between.

For your specific case though you don’t need any StructLayout. Not even when you have more than 1 field.

2 Likes

Thanks for the answer. It’s something I completely failed to understand.

Would there be a better alternative to removing the suppression error (other than using #pragma warning disable)?

If you really dig into this issue, you can find Unity employees blaming the IDEs for not respecting their suppressors. This warning is harmless. As the message implies, Unity tried to suppress the warning but the IDE was like “that’s cute, lol” and spat noise at you. Just ignore it until the two work it out.

3 Likes