How to Align a BaseField to Match the Alignment of Other Inspector Elements?

I’m working on aligning a BaseField to match the alignment of other objects in the Unity Inspector.

Here’s an example of how I’m declaring my ObjectField in a Editor class:

ObjectField objectField = new ObjectField(baseVariablesAsset.displayName)
{
    value = Target.BaseVariablesAsset,
    objectType = typeof(GraphVariables),
};

The resulting layout doesn’t align with other elements in the Inspector as expected.

image

I have tried to inspect the code and use UI Toolkit Debugger to find any style classes but haven’t found a clear solution.

Any help would be greatly appreciated, thanks in advance

Add the "unity-base-field__aligned" uss class to it. (you can access it through ObjectField.alignedFieldUssClassName or BaseField<Object>.alignedFieldUssClassName)


  public abstract class BaseField<TValueType> 
{
    // ...
     public static readonly string ussClassName = "unity-base-field";
     public static readonly string alignedFieldUssClassName = BaseField<TValueType>.ussClassName + "__aligned";
    // ...
}

Any BaseField<T> with that style applied, that is located inside the Inspector will align its label.

Also if under an element (or self) with "unity-inspector-element" class and another parent with "unity-inspector-main-container" class, the last one defines the available width.

2 Likes

It worked! Thank you :partying_face:

1 Like