Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice

com.unity.properties package

Discussion in 'Experimental Scripting Previews' started by sunfl0wr, Apr 10, 2022.

  1. sunfl0wr

    sunfl0wr

    Joined:
    Mar 11, 2015
    Posts:
    26
    Hi, as Unity started hiding all preview packages I've been worried about my use of this (com.unity.properties package) package in our game. What is the future plan of it? Should I be worried that it might disappear and I should start looking at making my own solutions for working data in a similar manner? Or will this become a public package at some point?

    (I hope I posted this in the correct category)
     
  2. martinpa_unity

    martinpa_unity

    Unity Technologies

    Joined:
    Oct 18, 2017
    Posts:
    479
    Hi!

    The com.unity.properties package recently released the 2.0.0 version of the package (available starting from 2021.3.0f1). If you are on this version or a newer version, I would advise you to update to this version because the package is being promoted as a module starting 2022.2 (based on the 2.0.0 version of the package).

    As a built-in module, it will not be necessary to add the package anymore, it should be available by installing the editor.

    I think this is the right place, I'm not entirely sure myself :)

    Hope this helps!
     
  3. sunfl0wr

    sunfl0wr

    Joined:
    Mar 11, 2015
    Posts:
    26
    Thank you! That sounds great! :)
    Will update to latest
     
  4. sunfl0wr

    sunfl0wr

    Joined:
    Mar 11, 2015
    Posts:
    26
    Hi again!
    I recently updated to the new 2.0 version and have been doing some experimentation.
    Recently I started doing some opimization and safety changes in my code and started making more struct immutable using readonly struct.
    Unfortunately it seems the GeneratePropertyBag attribute breaks on readonly structs. Is there any plan on fixing this?
     
  5. sunfl0wr

    sunfl0wr

    Joined:
    Mar 11, 2015
    Posts:
    26
    Hm after further experimenting it seems to be some combination of
    readonly struct and properties with only getters

    Example code:

    Code (CSharp):
    1.  
    2. [System.Serializable, Unity.Properties.GeneratePropertyBag]
    3. public readonly struct ReadOnlyStruct
    4. {
    5.    [field: UnityEngine.SerializeField, Unity.Properties.CreateProperty]
    6.    public int Test { get; }
    7.  
    8.    public ReadOnlyStruct(int test)
    9.    {
    10.       Test = test;
    11.    }
    12. }
    13.  
    Gives me this error in editor:

    Unity.Properties.SourceGenerator/Unity.Properties.SourceGenerator.PropertyBagGenerator/ReadOnlyStruct_110e67f1de864680bf4bb46816475795_PropertyBag.cs(15,24): error CS1520: Method must have a return type
     
  6. sunfl0wr

    sunfl0wr

    Joined:
    Mar 11, 2015
    Posts:
    26
    If I instead change it to this then I get no errors

    Code (CSharp):
    1.  
    2. [System.Serializable, Unity.Properties.GeneratePropertyBag]
    3. public readonly struct ReadOnlyStruct
    4. {
    5.     [field: UnityEngine.SerializeField, Unity.Properties.CreateProperty]
    6.     public readonly int Test;
    7.  
    8.     public ReadOnlyStruct(int test)
    9.     {
    10.         Test = test;
    11.     }
    12. }
    13.  
     
  7. sunfl0wr

    sunfl0wr

    Joined:
    Mar 11, 2015
    Posts:
    26
    Ah this seem to have nothing to do with readonly struct. Seem to be more of a general issue with properties with only getter or private setter

    Is this in the plans to fix or is this by design?
     
  8. martinpa_unity

    martinpa_unity

    Unity Technologies

    Joined:
    Oct 18, 2017
    Posts:
    479
    Hi @sunfl0wr!
    At the moment, the source generator doesn't support to create properties for the backing field of a C# property. It is certainly a bug that it creates a compile error (at most, it should either be ignore or produce a warning).

    I'll bring it up to the team to see if it is something we want to add. If we do add it, it would have the same weird name that C# will generate (something like
    Testk_BackingField
    , which is probably not super helpful). If we don't, we'll add a better error message for it.

    Some additional notes:
    • Using
      SerializeField
      will automatically attempt to create a property, so the
      CreateProperty
      attribute is not necessary in this case.
    • I don't think that
      readonly struct
      s are serializable in Unity, so unless you are serializing it through another way, it is safe to remove.
    • For your use-case, most of the time, what you will want to do is to serialize the backing field (since Unity can't serialize properties directly) and tag the getter property. In order to achieve this, you will need to do this:
    Code (CSharp):
    1. [System.Serializable, Unity.Properties.GeneratePropertyBag]
    2. public readonly struct ReadOnlyStruct
    3. {
    4.     // Serialize the field, but do not automatically create a property for it.
    5.     [field: UnityEngine.SerializeField, Unity.Properties.DontCreateProperty]
    6.     // Create a property for the getter.
    7.     [Unity.Properties.CreateProperty]
    8.     public int Test { get; }
    9.     public ReadOnlyStruct(int test)
    10.     {
    11.         Test = test;
    12.     }
    13. }
    The above should compile fine and work properly.
    Hope this helps!
     
    Wilhelm_LAS likes this.
  9. martinpa_unity

    martinpa_unity

    Unity Technologies

    Joined:
    Oct 18, 2017
    Posts:
    479
    In any case, can you report the bug using "Help \> Report a Bug..."?
    Thanks!
     
  10. Knedlo

    Knedlo

    Joined:
    Oct 15, 2012
    Posts:
    54
    Hi,

    I am trying to experiment with properties package and got stopped at the very beginning. I looked at https://docs.unity3d.com/Packages/com.unity.properties@1.7/manual/index.html and copy pasted the example to start with to my unity 2021.3.6f1 project. However it complains that PropertyContainer.Accept, IVisitPropertyAdapter and VisitContext don't exist. I tried multiple other versions of the package (2.1 and 1.8) but to no avail. 2.1 breaks a lot of things, but 1.8 behaves just like 1.7. I looked through older versions of the sample, but they also contain these. Any idea what could cause this?

    Edit: It actually works fine in a new project.
    Edit2: I coppied the manifest from my main project to the testing project and it still works with the same set of packages.
     
    Last edited: Sep 25, 2022
    martinpa_unity likes this.
  11. Wilhelm_LAS

    Wilhelm_LAS

    Joined:
    Aug 18, 2020
    Posts:
    53

    What is the purpose of the GeneratePropertyBag actually? Can you tell me with a basic example? I readed every documentation but didnt understood anything. Like what if i dont use that?
     
  12. martinpa_unity

    martinpa_unity

    Unity Technologies

    Joined:
    Oct 18, 2017
    Posts:
    479
    Hi @RiskyMelody,

    By default, property bags are generated using reflection the first time the type is used with the Properties module or package. In the module version or the 2.y.z version of the package, it is possible to use a source generator so that the property bag is generated ahead of time and will not use reflection anymore (reflection may still be used for private fields/properties or for private nested types). This code generation happens when a type is tagged with the
    [GeneratePropertyBag]
    attribute. Note that for code generation to kick-in for an assembly, the assembly must also be tagged with
    [assembly:GeneratePropertyBagsForAssembly]
    .

    Hope this helps!
     
    Wilhelm_LAS likes this.
  13. Kleptine

    Kleptine

    Joined:
    Dec 23, 2013
    Posts:
    282
    Hi,

    Is there any way to verify that a struct has a source-generated PropertyBag? I'm using the [GeneratePropertyBag] attribute, but it still seems to be using the reflection pathway. I'm not sure, though.
     
  14. martinpa_unity

    martinpa_unity

    Unity Technologies

    Joined:
    Oct 18, 2017
    Posts:
    479
    Hi @Kleptine, for the source generator to kick in, you must also tag the attribute with
    [assembly:GeneratePropertyBagsForAssembly]
    .

    Preferably, make the type partial to allow the generated code to access the internals, which will avoid the use of reflection even more.
     
  15. Kleptine

    Kleptine

    Joined:
    Dec 23, 2013
    Posts:
    282
    I'm already doing that, but I'm struggling to verify that it's working. What's the standard way to find out if a source generated property bag is actually getting used?
     
  16. martinpa_unity

    martinpa_unity

    Unity Technologies

    Joined:
    Oct 18, 2017
    Posts:
    479
    Usually, I use the IDE with the "Find Usages", but depending on the IDE you are using and the version you are using, it might not show up. The alternative is to get the property bag and look at its type. It will either be derived from
    ReflectedPropertyBag
    or will use a very code-generated name to it.
     
    Kleptine likes this.