Objective

Main objective of this blog post is to give you an idea about how to Attribute In Unity.

 

 

Step 1 Attributes

“Attribute is a declarative tag that is used to convey information about the behaviour of various elements like class, method and property in your program”

We can declare the attribute by using square brackets ([ ]) which are placed above the element it is used for.

 

HideInInspector

It is used to hide elements in inspector. By default elements have access modifier public that is visible in inspector while it not the case with private. It is used when we have public element that we don’t want to show in inspector.

Ex:

using UnityEngine;
using System.Collections;
public class HideInInspectorTest : MonoBehaviour {
public int visible;//visible in inspector
[HideInInspector]
public int hidden;//hidden in inspector
}


hide-in-inspector-test-script

 

1.2 Range

Range attribute is used to limit the values of variable.
Syntax: [Range(LowerLimit,UpperLimit)] Ex:

[Range(0,10)]
public int range;

range-test-script

 

1.3 Header

It is used to categorize our properties. Using header we can separate properties according to the header name.Ex:

[Header("Score")]//header Score
public int highScore;
public int currentScore;
[Header("Player")]//header Player
public int health;
public int level;

 header-test-script

 

1.4 Tooltip

Tooltip attribute shows additional information about element when we hover it over any element. It gives description about field.Ex

[Tooltip("Input  for Rotation(0° to 360°)")]
public int rotation;

 tooltip-test-script

 

1.5 ContextMenuItem

It is used to display the menu when right clicked on any element. It has two parameter items which are shown in inspector and method that will be called on click.Following reset menu item is used when we want to set the default value of integer variable named gravity.Ex:

[ContextMenuItem("Reset", "resetTheValue")]//Reset is shown on right-click
public float gravity;
private void resetTheValue(){//reset value to 9.8 on Reset
gravity = 9.8f;
}

context-menu-item-test-script  
content-menu-item-test-script
content-menu-item-test-script-gravity

 

1.6 Space

Space attribute is used to leave space between any of the two properties. It can be used to group properties by leaving space in between two groups.Ex.

[Space(10)]
public float gameTime;
public float remainingTime;
[Space(10)]
public bool isGamePause;
public bool isGameOver;
[Space(10)]
public int score;
public int highScore;

space-test-script

 

1.7 Multiline

If we have a public variable with datatype string, we can only assign single line string to that string variable. We can’t even use \n for new line. The Solution to this is to use a multiline attribute.

Syntax: [Multiline ()] or [Multiline]

If we want to show a particular number of lines then that number of lines is passed as parameter.
Note that passing number of lines is optional and by default it is 3.
Ex: [Multiline (5)]
Ex:

[Multiline]
public string stringVar;

multiline-test-script

 

1.8 System.Serializable

It is used to show a class with sub-property in inspector just like other elements (int, float, Vector3). Without this attribute you will not be able to see or edit objects of class in inspector.Suppose if you have a list that contains objects of class and you want to make that list editable from the inspector then you have to add that attribute to the class which you want to show in inspector.Ex:

[System.Serializable]
public class WayPoint {
public Vector3 position;
public bool isItStart;
public bool isItIntermediate;
public bool isItEnd;
}
public List wayPointList = new List ();

serializable-test-script

I hope you find this blog post is very helpful while working with Unity. Let me know in comment if you have any questions regarding Unity. I will reply you ASAP.

Got an Idea of Game Development? What are you still waiting for? Contact us now and see the Idea live soon. Our company has been named as one of the best Game Development Company in India.

I am Professional Game Developer, I love to develop a Game and also i love play a game, And i am always ready to helping you in Game Development.

face mask Belial The Demon Headgear Pocket Staff Magic