NaughtyAttributes
Attribute Extensions for Unity
Install / Use
/learn @dbrizov/NaughtyAttributesREADME
NaughtyAttributes
NaughtyAttributes is an extension for the Unity Inspector.
It expands the range of attributes that Unity provides so that you can create powerful inspectors without the need of custom editors or property drawers. It also provides attributes that can be applied to non-serialized fields or functions.
Most of the attributes are implemented using Unity's CustomPropertyDrawer, so they will work in your custom editors.
The attributes that won't work in your custom editors are the meta attributes and some drawer attributes such as
ReorderableList, Button, ShowNonSerializedField and ShowNativeProperty.
If you want all of the attributes to work in your custom editors, however, you must inherit from NaughtyInspector and use the NaughtyEditorGUI.PropertyField_Layout function instead of EditorGUILayout.PropertyField.
System Requirements
Unity 2019.4 or later versions. Don't forget to include the NaughtyAttributes namespace.
Installation
- The package is available on the openupm registry. You can install it via openupm-cli.
openupm add com.dbrizov.naughtyattributes
- You can also install via git url by adding this entry in your manifest.json
"com.dbrizov.naughtyattributes": "https://github.com/dbrizov/NaughtyAttributes.git#upm"
- You can also download it from the Asset Store
Documentation
Support
NaughtyAttributes is an open-source project that I am developing in my free time. If you like it you can support me by donating.
Overview
Special Attributes
AllowNesting
This attribute must be used in some cases when you want meta attributes to work inside serializable nested structs or classes. You can check in which cases you need to use it here.
public class NaughtyComponent : MonoBehaviour
{
public MyStruct myStruct;
}
[System.Serializable]
public struct MyStruct
{
public bool enableFlag;
[EnableIf("enableFlag")]
[AllowNesting] // Because it's nested we need to explicitly allow nesting
public int integer;
}
Drawer Attributes
Provide special draw options to serialized fields. A field can have only one DrawerAttribute. If a field has more than one, only the bottom one will be used.
AnimatorParam
Select an Animator paramater via dropdown interface.
public class NaughtyComponent : MonoBehaviour
{
public Animator someAnimator;
[AnimatorParam("someAnimator")]
public int paramHash;
[AnimatorParam("someAnimator")]
public string paramName;
}

Button
A method can be marked as a button. A button appears in the inspector and executes the method if clicked. Works both with instance and static methods.
public class NaughtyComponent : MonoBehaviour
{
[Button]
private void MethodOne() { }
[Button("Button Text")]
private void MethodTwo() { }
}

CurveRange
Set bounds and modify curve color for AnimationCurves
public class NaughtyComponent : MonoBehaviour
{
[CurveRange(-1, -1, 1, 1)]
public AnimationCurve curve;
[CurveRange(EColor.Orange)]
public AnimationCurve curve1;
[CurveRange(0, 0, 5, 5, EColor.Red)]
public AnimationCurve curve2;
}

Dropdown
Provides an interface for dropdown value selection.
public class NaughtyComponent : MonoBehaviour
{
[Dropdown("intValues")]
public int intValue;
[Dropdown("StringValues")]
public string stringValue;
[Dropdown("GetVectorValues")]
public Vector3 vectorValue;
private int[] intValues = new int[] { 1, 2, 3, 4, 5 };
private List<string> StringValues { get { return new List<string>() { "A", "B", "C", "D", "E" }; } }
private DropdownList<Vector3> GetVectorValues()
{
return new DropdownList<Vector3>()
{
{ "Right", Vector3.right },
{ "Left", Vector3.left },
{ "Up", Vector3.up },
{ "Down", Vector3.down },
{ "Forward", Vector3.forward },
{ "Back", Vector3.back }
};
}
}

EnumFlags
Provides dropdown interface for setting enum flags.
public enum Direction
{
None = 0,
Right = 1 << 0,
Left = 1 << 1,
Up = 1 << 2,
Down = 1 << 3
}
public class NaughtyComponent : MonoBehaviour
{
[EnumFlags]
public Direction flags;
}

Expandable
Make scriptable objects expandable.
public class NaughtyComponent : MonoBehaviour
{
[Expandable]
public ScriptableObject scriptableObject;
}

HorizontalLine
public class NaughtyComponent : MonoBehaviour
{
[HorizontalLine(color: EColor.Red)]
public int red;
[HorizontalLine(color: EColor.Green)]
public int green;
[HorizontalLine(color: EColor.Blue)]
public int blue;
}

InfoBox
Used for providing additional information.
public class NaughtyComponent : MonoBehaviour
{
[InfoBox("This is my int", EInfoBoxType.Normal)]
public int myInt;
[InfoBox("This is my float", EInfoBoxType.Warning)]
public float myFloat;
[InfoBox("This is my vector", EInfoBoxType.Error)]
public Vector3 myVector;
}

InputAxis
Select an input axis via dropdown interface.
public class NaughtyComponent : MonoBehaviour
{
[InputAxis]
public string inputAxis;
}

Layer
Select a layer via dropdown interface.
public class NaughtyComponent : MonoBehaviour
{
[Layer]
public string layerName;
[Layer]
public int layerIndex;
}

MinMaxSlider
A double slider. The min value is saved to the X property, and the max value is saved to the Y property of a Vector2 field.
public class NaughtyComponent : MonoBehaviour
{
[MinMaxSlider(0.0f, 100.0f)]
public Vector2 minMaxSlider;
}

ProgressBar
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;
}

ReorderableList
Provides array type fields with an interface for easy reordering of elements.
public class NaughtyComponent : MonoBehaviour
{
[ReorderableList]
public int[] intArray;
[ReorderableList]
public List<float> floatArray;
}

ResizableTextArea
A resizable text area where you can see the whole text. Unlike Unity's Multiline and TextArea attributes where you can see only 3 rows of a given text, and in order to see it or modify it you have to manually scroll down to the desired row.
public class NaughtyComponent : MonoBehaviour
{
[ResizableTextArea]
public string resizableTextArea;
}

Scene
Select a scene from the build settings via dropdown interface.
public class NaughtyComponent : MonoBehaviour
{
[Scene]
public string bootScene; // scene name
[Scene]
public int tutorialScene; // scene index
}

ShowAssetPreview
Shows the texture preview of a given asset (Sprite, Prefab...).
public class NaughtyComponent : MonoBehaviour
{
[ShowAssetPreview]
public Sprite sprite;
[ShowAssetPreview(128, 128)]
public GameObject prefab;
}
