Back to Naughtyattributes

NaughtyAttributes

README.html

2.1.518.7 KB
Original Source

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. 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 2018.3.0 or later versions. Feel free to try older version. 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

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.

source
publicclassNaughtyComponent:MonoBehaviour{publicAnimatorsomeAnimator;

	[AnimatorParam("someAnimator")]publicintparamHash;

	[AnimatorParam("someAnimator")]publicstringparamName;
}

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.

source
publicclassNaughtyComponent:MonoBehaviour{
	[Button]privatevoidMethodOne() { }

	[Button("Button Text")]privatevoidMethodTwo() { }
}

CurveRange

Set bounds and modify curve color for AnimationCurves

source
publicclassNaughtyComponent:MonoBehaviour{
	[CurveRange(-1,-1,1,1)]publicAnimationCurvecurve;

	[CurveRange(EColor.Orange)]publicAnimationCurvecurve1;

	[CurveRange(0,0,5,5,EColor.Red)]publicAnimationCurvecurve2;
}

Provides an interface for dropdown value selection.

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

EnumFlags

Provides dropdown interface for setting enum flags.

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

Expandable

Make scriptable objects expandable.

source
publicclassNaughtyComponent:MonoBehaviour{
	[Expandable]publicScriptableObjectscriptableObject;
}

HorizontalLine

source
publicclassNaughtyComponent:MonoBehaviour{
	[HorizontalLine(color:EColor.Red)]publicintred;

	[HorizontalLine(color:EColor.Green)]publicintgreen;

	[HorizontalLine(color:EColor.Blue)]publicintblue;
}

InfoBox

Used for providing additional information.

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

InputAxis

Select an input axis via dropdown interface.

source
publicclassNaughtyComponent:MonoBehaviour{
	[InputAxis]publicstringinputAxis;
}

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.

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

ProgressBar

source
publicclassNaughtyComponent:MonoBehaviour{
	[ProgressBar("Health",300,EColor.Red)]publicinthealth=250;

	[ProgressBar("Mana",100,EColor.Blue)]publicintmana=25;

	[ProgressBar("Stamina",200,EColor.Green)]publicintstamina=150;
}

ReorderableList

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

source
publicclassNaughtyComponent:MonoBehaviour{
	[ReorderableList]publicint[]intArray;

	[ReorderableList]publicList<float>floatArray;
}

ReadOnly

Makes a field read only.

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

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.

source
publicclassNaughtyComponent:MonoBehaviour{
	[ResizableTextArea]publicstringresizableTextArea;
}

Scene

Select a scene from the build settings via dropdown interface.

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

ShowAssetPreview

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

source
publicclassNaughtyComponent:MonoBehaviour{
	[ShowAssetPreview]publicSpritesprite;

	[ShowAssetPreview(128,128)]publicGameObjectprefab;
}

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).

source
publicclassNaughtyComponent:MonoBehaviour{publicList<Transform>transforms;

	[ShowNativeProperty]publicintTransformsCount=\>transforms.Count;
}

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).

source
publicclassNaughtyComponent:MonoBehaviour{
	[ShowNonSerializedField]privateintmyInt=10;

	[ShowNonSerializedField]privateconstfloatPI=3.14159f;

	[ShowNonSerializedField]privatestaticreadonlyVector3CONST\_VECTOR=Vector3.one;
}

Tag

Select a tag via dropdown interface.

source
publicclassNaughtyComponent:MonoBehaviour{
	[Tag]publicstringtagField;
}

Meta Attributes

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

BoxGroup

Surrounds grouped fields with a box.

source
publicclassNaughtyComponent:MonoBehaviour{
	[BoxGroup("Integers")]publicintfirstInt;
	[BoxGroup("Integers")]publicintsecondInt;

	[BoxGroup("Floats")]publicfloatfirstFloat;
	[BoxGroup("Floats")]publicfloatsecondFloat;
}

Foldout

Makes a foldout group.

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

EnableIf / DisableIf

source
publicclassNaughtyComponent:MonoBehaviour{publicboolenableMyInt;

	[EnableIf("enableMyInt")]publicintmyInt;

	[EnableIf("Enabled")]publicfloatmyFloat;

	[EnableIf("NotEnabled")]publicVector3myVector;publicboolEnabled() {returntrue; }publicboolNotEnabled=\>false;
}

You can have more than one condition.

source
publicclassNaughtyComponent:MonoBehaviour{publicboolflag0;publicboolflag1;

	[EnableIf(EConditionOperator.And,"flag0","flag1")]publicintenabledIfAll;

	[EnableIf(EConditionOperator.Or,"flag0","flag1")]publicintenabledIfAny;
}

ShowIf / HideIf

source
publicclassNaughtyComponent:MonoBehaviour{publicboolshowInt;

	[ShowIf("showInt")]publicintmyInt;

	[ShowIf("AlwaysShow")]publicfloatmyFloat;

	[ShowIf("NeverShow")]publicVector3myVector;publicboolAlwaysShow() {returntrue; }publicboolNeverShow=\>false;
}

You can have more than one condition.

source
publicclassNaughtyComponent:MonoBehaviour{publicboolflag0;publicboolflag1;

	[ShowIf(EConditionOperator.And,"flag0","flag1")]publicintshowIfAll;

	[ShowIf(EConditionOperator.Or,"flag0","flag1")]publicintshowIfAny;
}

Label

Override default field label.

source
publicclassNaughtyComponent:MonoBehaviour{
	[Label("Short Name")]publicstringveryVeryLongName;

	[Label("RGB")]publicVector3vectorXYZ;
}

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.

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

Validator Attributes

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

MinValue / MaxValue

Clamps integer and float fields.

source
publicclassNaughtyComponent:MonoBehaviour{
	[MinValue(0),MaxValue(10)]publicintmyInt;

	[MinValue(0.0f)]publicfloatmyFloat;
}

Required

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

source
publicclassNaughtyComponent:MonoBehaviour{
	[Required]publicTransformmyTransform;

	[Required("Custom required text")]publicGameObjectmyGameObject;
}

ValidateInput

The most powerful ValidatorAttribute.

source
publicclass\_NaughtyComponent:MonoBehaviour{
	[ValidateInput("IsNotNull")]publicTransformmyTransform;

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