Customizing Generated Classes

By default, AutoFields uses predefined templates to generate standard boilerplate code, including auto-properties for each named element, constructors, and a FindFields method that uses UQueryExtensions.Q<T>() to locate elements by name.

To customize the generated code (e.g., add custom methods, change property styles, modify null-checking logic, or integrate with custom base classes), you can replace the default templates with your own.

Customization is done by providing custom class template and fields template in the Settings. Templates use a simple placeholder substitution and repetition system:

  • {0:Placeholder}: Substituted once with class-level values (e.g., namespace).

  • {0:Fields@ ... }: Marks a repeating section in the fields template; repeated once per field, with [0:FieldName] placeholders substituted per field. Curly braces { and } in the repeating section must be replaced with square braces [ and ].

  • [0:Field...]: Substituted per-field within repeating sections (e.g., field name).

  • To escape curly braces { and } use double braces: {{ for { and }} for a``}``.

Note

Templates are processed as verbatim C# code strings. Ensure proper escaping, indentation, and C# syntax. The utility escapes identifiers with @ where needed (e.g., @[0:FieldName]).

Default Templates

These are the built-in templates. Copy and modify them for customization.

Class Template

namespace {0:Namespace}
{{
   /// <summary>
   /// {0:ClassName}.
   /// </summary>
   public partial class @{0:ClassName}
   {{
      {0:StartMarker}
      {0:FieldsList}
      {0:EndMarker}

      /// <summary>
      /// Initializes a new instance of the <see cref="@{0:ClassName}"/> class.
      /// </summary>
      public @{0:ClassName}()
      {{
      }}

      /// <summary>
      /// Initializes a new instance of the <see cref="@{0:ClassName}"/> class.
      /// </summary>
      /// <param name="document">UI document.</param>
      public @{0:ClassName}(UnityEngine.UIElements.UIDocument document)
      {{
         FindFields(document.rootVisualElement);
      }}

      /// <summary>
      /// Initializes a new instance of the <see cref="@{0:ClassName}"/> class.
      /// </summary>
      /// <param name="root">Root element.</param>
      public @{0:ClassName}(UnityEngine.UIElements.VisualElement root)
      {{
         FindFields(root);
      }}

      /// <summary>
      /// Find fields.
      /// </summary>
      /// <param name="document">UI document.</param>
      public void FindFields(UnityEngine.UIElements.UIDocument document) => FindFields(document.rootVisualElement);
   }}
}}
Class Template Placeholders
  • {0:Namespace}

    The target namespace from Settings (e.g., MyProject.UIBindings).

  • {0:ClassName}

    The generated class name (PascalCase, derived from *.uxml file name).
    See Name Processor about the names customization.
  • {0:StartMarker}

    Opening content before fields. For already existing files, only the content between markers will be replaced with field data.

  • {0:FieldsList}

    Output from the fields template (includes properties and FindFields method).

  • {0:EndMarker}

    Closing content after fields.

Fields Template

{0:Fields@
   /// <summary>
   /// [0:FieldName].
   /// </summary>
   public [0:FieldType] @[0:FieldName]
   [[
      get;
      private set;
   ]]
}

/// <summary>
/// Find fields.
/// </summary>
/// <param name="root">Root element.</param>
public void FindFields(UnityEngine.UIElements.VisualElement root)
{{
{0:Fields@
   @[0:FieldName] = UnityEngine.UIElements.UQueryExtensions.Q<[0:FieldType]>(root, "[0:UIName]");
}
   #if UNITY_EDITOR
   if ({0:Fields@(@[0:FieldName] == null) || }false)
   {{
      var asset = root.visualTreeAssetSource;
      var name = asset != null
         ? asset.name
         : (!string.IsNullOrEmpty(root.name) ? root.name : "Unknown VisualTreeAsset");
      UnityEngine.Debug.LogWarning("Some or all fields are null. Please ensure that you are using the correct VisualElement: " + name, asset);
   }}
   #endif
}}
Fields Template Structure
  • Field declarations: Repeating block {0:Fields@ ... } generates one property per named VisualElement.

  • FindFields method: Non-repeating boilerplate, with repeating assignments and null checks inside.

  • Indentation is preserved literally.

Per-Field Placeholders ([0:Key] inside {0:Fields@ ... } blocks)
  • [0:FieldName]
    C# property name (PascalCase or camelCase, sanitized from UXML name attribute, e.g., MyButton).
    See Name Processor about the names customization.
  • [0:FieldType]

    VisualElement type (e.g., UnityEngine.UIElements.Button).

  • [0:UIName]

    Exact UXML name attribute value for querying (e.g., my-button).

Repeating Sections Examples
  • Assignments: {0:Fields@ @[0:FieldName] = ... } → one line per field.

  • Null check: {0:Fields@(@[0:FieldName] == null) || }(MyButton == null) || (OtherField == null) || ... false (trailing || false ensures valid if).

Customizing Templates

  1. Open the Settings window (Window / AutoFields / Settings or Edit / Projects Settings… / AutoFields).

  2. Locate the Class Template and Fields Template fields

  3. Specify your custom templates.

  4. Files will be automatically updated with new fields data. Class Template will affect only new files.

Tip

  • Use partial class to allow extensions in other files.

  • Add custom methods in the class template after {0:EndMarker}.

  • For base classes: Change public partial class @{0:ClassName} to public partial class @{0:ClassName} : MyBase.

Example Customizations

  • Editable Fields

    In fields template, replace private set; with set;.

  • Custom Null Handling

    Replace the #if UNITY_EDITOR block with try-catch or caching.

  • Field Backing Fields

    Change auto-property to:

    private [0:FieldType] _{0:FieldName.ToLower()};
    public [0:FieldType] @[0:FieldName] { get => _{0:FieldName.ToLower()}; }