Code Snippet: In-Line Vector3Field for Editors

After fighting with Unity for the better part of an hour, attempting to align the label for a Vector3Field in an editor window on the same horizontal line as the Vector3Field, I decided to tackle it with a different approach.

Before and After using the new class.
using UnityEditor;
using UnityEngine;

public static class EditorUtilities
{
    /// <summary>
    /// Draws an inline Vector3Field with label.
    /// </summary>
    /// <param name="label">String to be used as the label.</param>
    /// <param name="input">Initial Vector3 input for this field.</param>
    /// <param name="options">GUILayoutOption parameters<</param>
    /// <see href="https://docs.unity3d.com/ScriptReference/EditorGUILayout.Vector3Field.html">EditorGUILayout.Vector3Field</see>
    /// <returns>Vector3 value defined by this inspector field.</returns>
    public static Vector3 Vector3Field(string label, Vector3 input, GUILayoutOption[] options = null)
    {
        EditorGUILayout.BeginHorizontal();
        EditorGUILayout.PrefixLabel(label);
        Vector3 ret = input;
        if (options != null) ret = EditorGUILayout.Vector3Field("", input, options);
        else ret = EditorGUILayout.Vector3Field("", input);
        EditorGUILayout.EndHorizontal();
        return ret;
    }

    /// <summary>
    /// Draws an inline Vector3Field with label.
    /// </summary>
    /// <param name="label">GUIContent to be used as the label.</param>
    /// <param name="input">Initial Vector3 input for this field.</param>
    /// <param name="options">GUILayoutOption parameters<</param>
    /// <returns>Vector3 value defined by this inspector field.</returns>
    /// <see href="https://docs.unity3d.com/ScriptReference/EditorGUILayout.Vector3Field.html">EditorGUILayout.Vector3Field</see>
    public static Vector3 Vector3Field(GUIContent label, Vector3 input, GUILayoutOption[] options = null)
    {
        EditorGUILayout.BeginHorizontal();
        EditorGUILayout.PrefixLabel(label);
        Vector3 ret = input;
        if (options != null) ret = EditorGUILayout.Vector3Field("", input, options);
        else ret = EditorGUILayout.Vector3Field("", input);
        EditorGUILayout.EndHorizontal();
        return ret;
    }
}

Using this code is easy, here’s how we call it:

Vector3 tmpVector = EditorUtilities.Vector3Field("Position:", referenceValue);
if(tmpVector != referenceValue) referenceValue = tmpVector;

Hope you found this useful!