What is the purpose of the GeneratePropertyBag? com.unity.properties @2.1

https://docs.unity3d.com/Packages/com.unity.properties@2.1/manual/index.html

I read the documentation but i dont understand why should i use and when?
Can i request a simple example?

1 Like

I‘m interested to hear that too. I only vaguely understood what this package is meant to be helpful with or used for.

From my understanding, it code generates property metadata that you can access performantly at runtime without using reflection. With that metadata, you can more easily traverse structs and serialize/deserialize them automatically. I have a hunch Unity is going to start moving to that internally for editor serialization.

I plan on using it around next year when enough people have moved on from 2021.3 to 2022.3 in my own libraries for json and msgpack serialization. I already have tools written for it, but I’m going to piggy back off this library as one less thing I need to maintain. I trust if Unity is using it in their JSON serialization library, it should be reliable enough for me to use in mine.

EDIT:
As for specifically that attribute, AFAIK, that attribute just gives a heads up to the code generator that you want those property bags code generated by source generators.

2 Likes

Hi! Sorry for the long response time, I wasn’t aware of this thread.

The documentation of the package was very lacking. The package has been converted into a Unity module starting in 2022.2 and we’ve added a more thorough documentation for it here.

So as @ indicated, the [GeneratePropertyBag] attribute tells the source generator to generate code for its property bag instead of relying on reflection. Since this source generator can add to the oompilation/domain reload time, you must also tag the assembly with [assembly: GeneratePropertyBagsForAssembly] to opt-in this process.

Ideally, the type should also be made partial so that the generated code can access the internals of the type, which will avoid even more reflection.