Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

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

Discussion in 'Experimental Scripting Previews' started by Wilhelm_LAS, May 2, 2023.

  1. Wilhelm_LAS

    Wilhelm_LAS

    Joined:
    Aug 18, 2020
    Posts:
    35
    CodeSmile likes this.
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    4,006
    I‘m interested to hear that too. I only vaguely understood what this package is meant to be helpful with or used for.
     
  3. Deleted User

    Deleted User

    Guest

    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.
     
    Last edited by a moderator: May 2, 2023
    brunocoimbra and Wilhelm_LAS like this.
  4. martinpa_unity

    martinpa_unity

    Unity Technologies

    Joined:
    Oct 18, 2017
    Posts:
    359
    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 @jasonboukheir3 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.
     
    Last edited: Aug 15, 2023