Skip to content
TheCell's Blog
  • Home
  • Portfolio
  • Electronics and Programming

Styling Inspector Scripts with Unity Attributes

On 16. November 2019 by TheCell

To improve Inspector windows of scripts I summarized and created screenshots for several useful fields. A more advanced list of things can be found here: https://champview.wordpress.com/2014/12/12/a-summary-of-unity-attributes/ The whole script looks like this when watched in the inspector

public class ImprovedInspector : MonoBehaviour
{
	//[ContextMenuItem("Reset", "ResetBiography")]
	[Multiline(8)]
	[SerializeField] private string playerBiography = "";
	/*
	[ContextMenu("This script is intended to help you style your scripts")]
	private void ContextMenu()
	{
		Debug.Log("Do Something");
	}
	*/
	[Header("Health Settings")]
	[SerializeField] private int health = 0;
	[SerializeField] private int maxHealth = 100;

	[Header("Shield Settings")]
	[SerializeField] private int shield = 0;
	[SerializeField] private int maxShield = 0;

	[HideInInspector]
	public int changeAbleFromOutside = 1; // this will not show up in the inspector

	[Space(10)]
	[SerializeField] private ModelImporterIndexFormat model;
	private enum ModelImporterIndexFormat
	{
		Auto = 0,
		[InspectorName("16 bits")]
		UInt16 = 1,
		[InspectorName("32 bits")]
		UInt32 = 2,
	}

	[Space(100)]
	[SerializeField] private int spaceExample = 0;
	[TextArea(4, 8)] // or just [TextArea]
	[SerializeField] private string exampleField;
	[Tooltip("The field does nothing, really")]
	[SerializeField] private int mysteriousField = 1;
}

Serialize Field

With Serialize Field you can display private properties in the inspector. This means you can make classes which are well encapsulated from a programming point of view but still be customizeable from the Editor.
https://docs.unity3d.com/ScriptReference/SerializeField.html

[SerializeField] private int health = 0;

Header Fields

Header fields are nice to group fields.
https://docs.unity3d.com/ScriptReference/HeaderAttribute.html

[Header("Health Settings")]
[SerializeField] private int health = 0;
[SerializeField] private int maxHealth = 100;
[Header("Shield Settings")]
[SerializeField] private int shield = 0;
[SerializeField] private int maxShield = 0;

Hide Attributes

This is very nice if you need to have public attributes which should not show up in the Inspector. The normal approach would be to go with private by default and mark viewable fields with Serializefield. https://docs.unity3d.com/ScriptReference/HideInInspector.html

[HideInInspector]
public int changeAbleFromOutside = 1; // this will not show up in the inspector

Inspector Name

If you want custom names for the enumerations in your inspector this is a good way to do it.
https://docs.unity3d.com/ScriptReference/InspectorNameAttribute.html

[SerializeField] private ModelImporterIndexFormat model;
private enum ModelImporterIndexFormat
{
	Auto = 0,
	[InspectorName("16 bits")]
	UInt16 = 1,
	[InspectorName("32 bits")]
	UInt32 = 2,
}

Space

The Space attribute allows to add visual space in between inspector elements.
https://docs.unity3d.com/ScriptReference/SpaceAttribute.html

[Space(100)]
[SerializeField] private int spaceExample = 0;

Text Area

TextAreas are helpful if you want to put in a lot of text and still be able to see it. Optional you can define what the minimal lines are (here 4) and how many lines it should display before adding a scrollbar (here 8). https://docs.unity3d.com/ScriptReference/TextAreaAttribute.html

[TextArea(4, 8)] // or just [TextArea]
[SerializeField] private string exampleField;

Tooltip

Tooltips are a great way to explain what these attributes really do. Activated by hover over it.
https://docs.unity3d.com/ScriptReference/TooltipAttribute.html

[Tooltip("The field does nothing, really")]
[SerializeField] private int mysteriousField = 1;
Gamedev, Programming
Tags: Attribute Fields

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Meta

  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org

Categories

  • Allgemein
  • Fotos
  • Gamedev
  • games
  • HSLU
  • ImpressionsFrom
  • KVD
  • Papers
  • Persönlich
  • Portfolio
  • Programming
  • Zitate

Recent Posts

  • Unity EditorScripting
  • One way to handle and save Enums
  • The Impressions from Bugsnax
  • The Impressions from Manifold Garden
  • The Impressions from Sable

Archives

  • November 2023
  • December 2022
  • November 2022
  • February 2022
  • November 2021
  • October 2021
  • September 2021
  • July 2021
  • April 2021
  • March 2021
  • February 2021
  • January 2021
  • September 2020
  • July 2020
  • April 2020
  • March 2020
  • February 2020
  • December 2019
  • November 2019
  • October 2019
  • August 2019
  • June 2019
  • February 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • May 2018
  • March 2018
  • February 2018
  • December 2017
  • November 2017
  • September 2017
  • July 2017
  • June 2017
  • April 2017
  • February 2017
  • January 2017
  • October 2016
  • September 2016
  • July 2016
  • May 2016
  • April 2016
  • March 2016
  • August 2015
  • July 2015
  • May 2015
  • April 2015
  • March 2015
  • February 2015
  • January 2015
  • October 2014
  • April 2014
  • March 2014
  • February 2014

Meta

  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org

Copyright TheCell's Blog 2025 | Theme by Theme in Progress | Proudly powered by WordPress