Guid serialization and Burst

Guid is Burstable. Since Unity doesn’t serialize Guid on its own I have been serializing Guid to a string using OnBeforeSerialize and OnAfterDeserialize. However, a string isn’t Burstable and I can’t find a way to get Burst to ignore these fields used only for serialization.

Is there anything I can easily serialize a Guid with? Either will Unity’s built-in serialization or the prior-mentioned callbacks that is also compatible with Burst?

What’s the containing code like? Assuming it’s ISerializationCallbackReceiver you’re talking about, that only works on classes I think, so that’s something Burst isn’t going to touch anyway. Which confuses me a little with this problem. You’d pass the Guid or some inner struct to Burst and that would not contain the string field that’s on the class, so presumably it would be fine with Burst.

Ahhh … I had switched this code from a class to a struct as I migrated to Jobs and Burst.

If I remember correctly, the serialization callbacks were still getting called though, so I mistakenly thought that worked. But as you point out and the ISerializationCallbackReceiver docs state “The callback interface only works with classes. It does not work with structs.”

This also answers another problem I am working on.

Thanks for the reply.

you could create a custom guid struct or class that’s serializable and has operators that convert it to/from Guid.

Not my solution, but Chat GPT recommended this alternative which splits the GUID into two longs. You’d have to create the GUID in managed code, but at least it would serialize. You could then implement an IEquatable interface to give you an “== or !=” operator capability. Your mileage may vary, and I have not tried this myself yet since I found this thread trying to validate the suggestion (which I recommend you always do with AI code).

[Serializable]
public struct SimpleGuid
{
    public long Part1;
    public long Part2;

    public static SimpleGuid Create()
    {
        var guid = Guid.NewGuid();
        byte[] bytes = guid.ToByteArray();
        return new SimpleGuid
        {
            Part1 = BitConverter.ToInt64(bytes, 0),
            Part2 = BitConverter.ToInt64(bytes, 8)
        };
    }
}

Later in the day I posted this (sorry it is on a stale thread) I read that Unity will be converting Entity references to “globally unique” identifiers. Right now they are long-int, which are blitable. As stated, GUID and UUIDs are not, so I am very interested in learning what they plan on using for “globally unique” IDs. If they have(are) added(ing) a blitable GUID to burst I’d recommend using whatever they use as a datatype and take advantage of all the research and testing they’ll have put into it.