Purpose: To outline and share tools to clean up inspector menus created by scripts. These are more correctly known as Attributes and are available in the Unity Documentation.
When discussing the development of robust Unity Inspector menus, I utilize various built-in methods to ensure clarity. For each item covered, I have included a supplementary link in the title that directs readers to the official Unity documentation for those seeking more in-depth technical information.
Note: Variables that are public will automatically show up in the inspector menu. Otherwise [Serialize Field] must be used. Avoid overusing public variables.
Written: [Header("Button Configuration")]
Headers are essential for structuring your Unity Inspector menus. They allow you to clearly segment and categorize adjustable variables, grouping related settings together to provide immediate context and enhance organization.
Written: [Range(Min, Max)]
The Range attribute provides a simple way to control the minimum and maximum values of an Integer (Int) directly in the Unity Inspector. This capability significantly streamlines development, especially for iteration. For example, it allows you to quickly tune variables like the number of items to instantiate or game mode score limits. By avoiding the need to reopen your Integrated Development Environment (IDE) and recompile code every time you adjust these values, you can drastically speed up the testing process.
Written: [Space(Height)]
Adds vertical space, numbered in pixels, above the decorated field for better spacing between groups of variables.
Written: [Tooltip("Example Text")]
Displays a text message when the user hovers the mouse over the field's label in the Inspector.
Written: [TextArea(Min, Max)]
Turns a standard string field into a flexible scrollable text area with specified minimum and maximum visible lines.
Written: [HideInInspector]
Prevents a public or [SerializeField] field from being displayed in the Inspector, while still allowing it to be serialized.
Written: [SerializeField]
The [SerializeField] attribute is one of the most fundamental and frequently used attributes in Unity development. Its primary purpose is to force the Unity Editor to serialize a private or protected field, allowing its value to be saved with the scene or prefab and making it editable in the Inspector window.
public class InspectorElementsExamples : UdonSharpBehaviour
{
[Header("Inspector Example Configuration")]
[Range(0, 15)] public int Score;
//https://docs.unity3d.com/6000.2/Documentation/ScriptReference/SpaceAttribute.html
[Space(20)] //An added space in-inspector of 20 pixels
//https://docs.unity3d.com/ScriptReference/TooltipAttribute.html
[Tooltip("Example Text")]
//https://docs.unity3d.com/6000.2/Documentation/ScriptReference/TextAreaAttribute.html
[TextArea(1, 20)] public string ExampleTextArea = "Hey there!";
//https://docs.unity3d.com/6000.2/Documentation/ScriptReference/HideInInspector.html
[HideInInspector] public int ExampleHidden;
//https://docs.unity3d.com/6000.2/Documentation/ScriptReference/SerializeField.html
[SerializeField] float ShownVariable = 2.0f;
[SerializeField]
float SecondShownVariable;
}