Skip to content

dbrizov/NaughtyAttributes

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Repository files navigation

NaughtyAttributes

Unity 2019.4+openupmLicense: MIT

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

  1. The package is available on the openupm registry. You can install it via openupm-cli.
openupm add com.dbrizov.naughtyattributes 
  1. 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" 
  1. 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.

publicclassNaughtyComponent:MonoBehaviour{publicMyStructmyStruct;}[System.Serializable]publicstructMyStruct{publicboolenableFlag;[EnableIf("enableFlag")][AllowNesting]// Because it's nested we need to explicitly allow nestingpublicintinteger;}

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.

publicclassNaughtyComponent:MonoBehaviour{publicAnimatorsomeAnimator;[AnimatorParam("someAnimator")]publicintparamHash;[AnimatorParam("someAnimator")]publicstringparamName;}

inspector

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.

publicclassNaughtyComponent:MonoBehaviour{[Button]privatevoidMethodOne(){}[Button("Button Text")]privatevoidMethodTwo(){}}

inspector

CurveRange

Set bounds and modify curve color for AnimationCurves

publicclassNaughtyComponent:MonoBehaviour{[CurveRange(-1,-1,1,1)]publicAnimationCurvecurve;[CurveRange(EColor.Orange)]publicAnimationCurvecurve1;[CurveRange(0,0,5,5,EColor.Red)]publicAnimationCurvecurve2;}

inspector

Dropdown

Provides an interface for dropdown value selection.

publicclassNaughtyComponent:MonoBehaviour{[Dropdown("intValues")]publicintintValue;[Dropdown("StringValues")]publicstringstringValue;[Dropdown("GetVectorValues")]publicVector3vectorValue;privateint[]intValues=newint[]{1,2,3,4,5};privateList<string>StringValues{get{returnnewList<string>(){"A","B","C","D","E"};}}privateDropdownList<Vector3>GetVectorValues(){returnnewDropdownList<Vector3>(){{"Right",Vector3.right},{"Left",Vector3.left},{"Up",Vector3.up},{"Down",Vector3.down},{"Forward",Vector3.forward},{"Back",Vector3.back}};}}

inspector

EnumFlags

Provides dropdown interface for setting enum flags.

publicenumDirection{None=0,Right=1<<0,Left=1<<1,Up=1<<2,Down=1<<3}publicclassNaughtyComponent:MonoBehaviour{[EnumFlags]publicDirectionflags;}

inspector

Expandable

Make scriptable objects expandable.

publicclassNaughtyComponent:MonoBehaviour{[Expandable]publicScriptableObjectscriptableObject;}

inspector

HorizontalLine

publicclassNaughtyComponent:MonoBehaviour{[HorizontalLine(color:EColor.Red)]publicintred;[HorizontalLine(color:EColor.Green)]publicintgreen;[HorizontalLine(color:EColor.Blue)]publicintblue;}

inspector

InfoBox

Used for providing additional information.

publicclassNaughtyComponent:MonoBehaviour{[InfoBox("This is my int",EInfoBoxType.Normal)]publicintmyInt;[InfoBox("This is my float",EInfoBoxType.Warning)]publicfloatmyFloat;[InfoBox("This is my vector",EInfoBoxType.Error)]publicVector3myVector;}

inspector

InputAxis

Select an input axis via dropdown interface.

publicclassNaughtyComponent:MonoBehaviour{[InputAxis]publicstringinputAxis;}

inspector

Layer

Select a layer via dropdown interface.

publicclassNaughtyComponent:MonoBehaviour{[Layer]publicstringlayerName;[Layer]publicintlayerIndex;}

inspector

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.

publicclassNaughtyComponent:MonoBehaviour{[MinMaxSlider(0.0f,100.0f)]publicVector2minMaxSlider;}

inspector

ProgressBar

publicclassNaughtyComponent:MonoBehaviour{[ProgressBar("Health",300,EColor.Red)]publicinthealth=250;[ProgressBar("Mana",100,EColor.Blue)]publicintmana=25;[ProgressBar("Stamina",200,EColor.Green)]publicintstamina=150;}

inspector

ReorderableList

Provides array type fields with an interface for easy reordering of elements.

publicclassNaughtyComponent:MonoBehaviour{[ReorderableList]publicint[]intArray;[ReorderableList]publicList<float>floatArray;}

inspector

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.

publicclassNaughtyComponent:MonoBehaviour{[ResizableTextArea]publicstringresizableTextArea;}

inspector

Scene

Select a scene from the build settings via dropdown interface.

publicclassNaughtyComponent:MonoBehaviour{[Scene]publicstringbootScene;// scene name[Scene]publicinttutorialScene;// scene index}

inspector

ShowAssetPreview

Shows the texture preview of a given asset (Sprite, Prefab...).

publicclassNaughtyComponent:MonoBehaviour{[ShowAssetPreview]publicSpritesprite;[ShowAssetPreview(128,128)]publicGameObjectprefab;}

inspector

ShowNativeProperty

Shows native C# properties in the inspector. All native properties are displayed at the bottom of the inspector after the non-serialized fields and before the method buttons. It supports only certain types (bool, int, long, float, double, string, Vector2, Vector3, Vector4, Color, Bounds, Rect, UnityEngine.Object).

publicclassNaughtyComponent:MonoBehaviour{publicList<Transform>transforms;[ShowNativeProperty]publicintTransformsCount=>transforms.Count;}

inspector

ShowNonSerializedField

Shows non-serialized fields in the inspector. All non-serialized fields are displayed at the bottom of the inspector before the method buttons. Keep in mind that if you change a non-static non-serialized field in the code - the value in the inspector will be updated after you press Play in the editor. There is no such issue with static non-serialized fields because their values are updated at compile time. It supports only certain types (bool, int, long, float, double, string, Vector2, Vector3, Vector4, Color, Bounds, Rect, UnityEngine.Object).

publicclassNaughtyComponent:MonoBehaviour{[ShowNonSerializedField]privateintmyInt=10;[ShowNonSerializedField]privateconstfloatPI=3.14159f;[ShowNonSerializedField]privatestaticreadonlyVector3CONST_VECTOR=Vector3.one;}

inspector

SortingLayer

Select a sorting layer via dropdown interface.

publicclassNaughtyComponent:MonoBehaviour{[SortingLayer]publicstringlayerName;[SortingLayer]publicintlayerId;}

inspector

Tag

Select a tag via dropdown interface.

publicclassNaughtyComponent:MonoBehaviour{[Tag]publicstringtagField;}

inspector

Meta Attributes

Give the fields meta data. A field can have more than one meta attributes.

BoxGroup

Surrounds grouped fields with a box.

publicclassNaughtyComponent:MonoBehaviour{[BoxGroup("Integers")]publicintfirstInt;[BoxGroup("Integers")]publicintsecondInt;[BoxGroup("Floats")]publicfloatfirstFloat;[BoxGroup("Floats")]publicfloatsecondFloat;}

inspector

Foldout

Makes a foldout group.

publicclassNaughtyComponent:MonoBehaviour{[Foldout("Integers")]publicintfirstInt;[Foldout("Integers")]publicintsecondInt;}

inspector

EnableIf / DisableIf

publicclassNaughtyComponent:MonoBehaviour{publicboolenableMyInt;[EnableIf("enableMyInt")]publicintmyInt;[EnableIf("Enabled")]publicfloatmyFloat;[EnableIf("NotEnabled")]publicVector3myVector;publicboolEnabled(){returntrue;}publicboolNotEnabled=>false;}

inspector

You can have more than one condition.

publicclassNaughtyComponent:MonoBehaviour{publicboolflag0;publicboolflag1;[EnableIf(EConditionOperator.And,"flag0","flag1")]publicintenabledIfAll;[EnableIf(EConditionOperator.Or,"flag0","flag1")]publicintenabledIfAny;}

ShowIf / HideIf

publicclassNaughtyComponent:MonoBehaviour{publicboolshowInt;[ShowIf("showInt")]publicintmyInt;[ShowIf("AlwaysShow")]publicfloatmyFloat;[ShowIf("NeverShow")]publicVector3myVector;publicboolAlwaysShow(){returntrue;}publicboolNeverShow=>false;}

inspector

You can have more than one condition.

publicclassNaughtyComponent:MonoBehaviour{publicboolflag0;publicboolflag1;[ShowIf(EConditionOperator.And,"flag0","flag1")]publicintshowIfAll;[ShowIf(EConditionOperator.Or,"flag0","flag1")]publicintshowIfAny;}

Label

Override default field label.

publicclassNaughtyComponent:MonoBehaviour{[Label("Short Name")]publicstringveryVeryLongName;[Label("RGB")]publicVector3vectorXYZ;}

inspector

OnValueChanged

Detects a value change and executes a callback. Keep in mind that the event is detected only when the value is changed from the inspector. If you want a runtime event, you should probably use an event/delegate and subscribe to it.

publicclassNaughtyComponent:MonoBehaviour{[OnValueChanged("OnValueChangedCallback")]publicintmyInt;privatevoidOnValueChangedCallback(){Debug.Log(myInt);}}

ReadOnly

Make a field read only.

publicclassNaughtyComponent:MonoBehaviour{[ReadOnly]publicVector3forwardVector=Vector3.forward;}

inspector

Validator Attributes

Used for validating the fields. A field can have infinite number of validator attributes.

MinValue / MaxValue

Clamps integer and float fields.

publicclassNaughtyComponent:MonoBehaviour{[MinValue(0),MaxValue(10)]publicintmyInt;[MinValue(0.0f)]publicfloatmyFloat;}

inspector

Required

Used to remind the developer that a given reference type field is required.

publicclassNaughtyComponent:MonoBehaviour{[Required]publicTransformmyTransform;[Required("Custom required text")]publicGameObjectmyGameObject;}

inspector

ValidateInput

The most powerful ValidatorAttribute.

publicclass_NaughtyComponent:MonoBehaviour{[ValidateInput("IsNotNull")]publicTransformmyTransform;[ValidateInput("IsGreaterThanZero","myInteger must be greater than zero")]publicintmyInt;privateboolIsNotNull(Transformtr){returntr!=null;}privateboolIsGreaterThanZero(intvalue){returnvalue>0;}}

inspector