You can generate a partial C# class from the UXML file. This class will contain field declarations and the method that assigns them. As long as this generated class exists it will be automatically regenerated on each change to the corresponding UXML file.
- Select the UXML file
- In the inspector window, select Generate .gen.cs or Generate .gen.cs + .cs menu item from Options dropdown menu
The namespace of a class is determined by the following factors:
- If
gen-cs-namespace
attribute in theUXML
tag of the UXML file exists, its value will be used. This can be set from the UXML importer inspector header. - If
AssemblyDefinitionAsset
orAssemblyDefinitionReferenceAsset
exists in the UXML directory or parent directory:AssemblyDefinitionAsset.rootNamespace
will be used. - If
AssemblyDefinitionAsset.rootNamespace
is emptyAssemblyDefinitionAsset.name
will be used instead. - If the UXML file is inside the Assets folder, the
EditorSettings.projectGenerationRootNamespace
will be used. - If none of the above conditions are met, the class will have no namespace.
Settings for code generation can be modified from the project settings window. Project Settings/UI Code Generation
MS Docs. In short a partial class allows you to split class definition over more than one file, it is usually used with code generation features.
An example of partial class usage can be found in the package sample "UI Code Generation". Available in the Package Manager Window.