NaughtyAttributes

repository·master·Indexed 26 days ago

https://github.com/dbrizov/naughtyattributes

A Unity Inspector extension (version 2.1.6) that expands the range of available attributes, allowing developers to create functional inspectors without writing custom editors or property drawers. It supports attributes on non-serialized fields and functions, providing tools for conditional visibility (ShowIf, HideIf), validation (Required, ValidateInput), layout (BoxGroup, Foldout), and specialized input interfaces (Dropdown, MinMaxSlider, ProgressBar).

Tokens
5.7K
Snippets
33
Records
43
Agent score
40%

What's inside NaughtyAttributes

  1. Ensure attribute compatibility in Custom Editors

    master

    Most attributes use Unity's CustomPropertyDrawer and work in custom editors. However, meta attributes and certain drawer attributes (ReorderableList, Button, ShowNonSerializedField, and ShowNativeProperty) will NOT work in standard custom editors.

    To make all attributes work in your custom editors:

    1. Inherit from NaughtyInspector.
    2. Use NaughtyEditorGUI.PropertyField_Layout instead of EditorGUILayout.PropertyField.
  2. Integrate NaughtyAttributes with Custom Editors

    master

    Most NaughtyAttributes are implemented using Unity's CustomPropertyDrawer, meaning they will work automatically in your custom editors.

    However, if you want all attributes to work within a custom editor, you must:

    1. Inherit your editor from NaughtyInspector.
    2. Use NaughtyEditorGUI.PropertyField_Layout instead of EditorGUILayout.PropertyField to layout your fields.
  3. Conditionally enable or disable fields with EnableIf / DisableIf

    master

    Use [EnableIf("fieldName")] or [DisableIf("fieldName")] to enable or disable a field in the Inspector based on the value of another field, property, or method.

    You can also use logical operators by providing an EConditionOperator and multiple field names.

    Supported operators via EConditionOperator:

    • EConditionOperator.And (all conditions must be true)
    • EConditionOperator.Or (any condition must be true)
    public class NaughtyComponent : MonoBehaviour
    {
    	public bool flag0;
    	public bool flag1;
    
    	[EnableIf(EConditionOperator.And, "flag0", "flag1")]
    	public int enabledIfAll;
    
    	[EnableIf(EConditionOperator.Or, "flag0", "flag1")]
    	public int enabledIfAny;
    }
  4. Mark fields as required with Required

    master

    Use the [Required] attribute to visually remind developers that a reference type field must be assigned. You can provide a custom error message string: [Required("Custom message")].

    public class NaughtyComponent : MonoBehaviour
    {
    	[Required]
    	public Transform myTransform;
    
    	[Required("Custom required text")]
    	public GameObject myGameObject;
    }
  5. Use ProgressBar attribute

    master

    Displays a progress bar in the inspector. Requires a label string, a max value, and an EColor.

    public class NaughtyComponent : MonoBehaviour
    {
    	[ProgressBar("Health", 300, EColor.Red)]
    	public int health = 250;
    
    	[ProgressBar("Mana", 100, EColor.Blue)]
    	public int mana = 25;
    
    	[ProgressBar("Stamina", 200, EColor.Green)]
    	public int stamina = 150;
    }
  6. Use SortingLayer attribute

    master

    Provides a dropdown interface to select a Sorting Layer. Can be used for string (layer name) or int (layer ID) fields.

    public class NaughtyComponent : MonoBehaviour
    {
    	[SortingLayer]
    	public string layerName;
    
    	[SortingLayer]
    	public int layerId;
    }