How can I make a custom property drawer see a property defined in a code-generated class?

I am following this Unity - Manual: Create and use a source generator to add methods and properties to my custom control. For example, my custom control looks like this:

    [UxmlElement("TestCustomControl")]
    public partial class TestCustomControl : VisualElement
    {
        [UxmlAttribute]
        public string MyString { get; set; } = MYSTRING_DEFAULT_VALUE;

        [UxmlAttribute]
        public int MyInt { get; set; } = MYINT_DEFAULT_VALUE;
        // ... etc

and my code-generated class looks like this:

public partial class TestCustomControl : VisualElement
{
    [UxmlAttribute]
    public string PROPERTY_I_AM_LOOKING_FOR { get; set; } = ""I am looking for this"";
    // ... etc

If I put a breakpoint in my TestCustomControl() constuctor I can see the property defined in the code-generated class exists, for example:

However, the property does not exist in the boxedValues in a custom property drawer, for example:


The ‘MyInt’ and ‘MyString’ properties defined in my custom control are there BUT the ‘PROPERTY_I_AM_LOOKING_FOR’ property from the code-generated class is Not there.

I am accessing the parent property in my custom property drawer by doing:

            string parentPropertyPath = property.propertyPath[..property.propertyPath.LastIndexOf('.')];
            SerializedProperty parent = property.serializedObject.FindProperty(parentPropertyPath);

So, my question is: Is it possible for a custom property drawer see the properties defined in a code-generated class?

We also use a code generator to generate the UxmlAtrribute fields, so I suspect it is not being run after your code generator.

You may need to add a dependency, like this

Thank you for your reply :slightly_smiling_face:. I added the reference to Unity.UIToolkit.SourceGenerator.dll in the codegen project as in:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.3.1" />
  </ItemGroup>

  <ItemGroup>
      <Analyzer Include="C:\Program Files\Unity\Hub\Editor\6000.0.24f1\Editor\Data\Tools\Unity.SourceGenerators\Unity.UIToolkit.SourceGenerator.dll"/>
  </ItemGroup>

</Project>

Generated a new dll and placed it in my Unity project. Unfortunately, the code-generated property with [UxmlAttribute] attribute still does not appear as a serialized property in the custom property drawer boxedValues. I also tried referencing the other Unity.SourceGenerators dlls as in:


without success. The [UxmlAttribute] property in my code-generation class still does not appear as a boxed value in the custom property drawer.
What else do you suggest me to try?

Does it show up if you dont use a custom property drawer?

Thank you for the follow-up :slight_smile:.
Unfortunately, no, the [UxmlAttribute] property in the code-gen class does not appear in UIBuilder inspector after stopping using the custom property drawer.

That sounds like it’s not running the source generator on the attributes you generated. It could be a bug on our end, this is not something we have tested. Please file a bug report so we can look into it

1 Like

Done, filed the bug report: UI Toolkit custom property drawers do not pickup properties with [UXMLAttribute] attribute defined in a code-generated classes. - Unity Bug Reporting Portal - Jira Service Management

1 Like