In this volume #2 of Unity tips, I’m gonna share with you 5 useful attributes to use in your script. (5 Editor Tips – Unity Tips (Volume #1))
Every Tuesday, Unity developers across the globe share some useful tips about Unity. Check #UnityTips to see it yourself!
#6 Require Component
Most of the times we use some components in our scripts.
You can ensure that whoever uses the script will have the components needed by using the “RequireComponent” attribute.
What does it do? It will add that component to the object instantly.
[RequireComponent(typeof(ComponentName))]
Unity tip of the day:
β Omar Balfaqih (@OBalfaqih) February 12, 2019
Using “RequireComponent” will always make sure that the object has that component if not, it will add it automatically.
You can watch the full video with all 5 attribute tips here: https://t.co/7PaT01mRMn#UnityTips #GameDev #Unity3D pic.twitter.com/ww7uWnPhvC
#7 Add Component Menu
You can add your script to the component menu with a custom path starting from the “Component” as the root.
Use “AddComponentMenu” attribute.
[AddComponentMenu("Path/To/Script")]
Unity tip of the day:
β Omar Balfaqih (@OBalfaqih) February 14, 2019
You can have your own component menu to add your script.
To do so, use the attribute “AddComponentMenu” then pass the path (Component is the root).
Watch the full video with 5 Attribute Tips (#UnityTips Volume #2):https://t.co/7PaT01mRMn#Unity3D #GameDev pic.twitter.com/0ldMpvl61X
#8 Header
To make your script user-friendly in the editor, you can add headers on top of the variables using the “Header” attribute.
[Header("Title")]
Unity tip of the day:
β Omar Balfaqih (@OBalfaqih) February 18, 2019
You can add headers or titles to your variables.
Use “Header” attribute.
Watch the full video of 5 Attribute Tips – Unity Tips(Volume #2):https://t.co/7PaT01mRMn#UnityTips #GameDev #Unity3D pic.twitter.com/Olcbg0WjBX
#9 Range
In some cases, you want the variables to stick between two values, for example, the player’s health should be between 0 and 100.
Luckily there’s an attribute for that which is “Range”. You have to place it on top of the affected variable.
It will limit the variable and provide a slider.
[Range(0, 100)]
public float health;
Unity tip of the day:
β Omar Balfaqih (@OBalfaqih) February 19, 2019
Use “Range” attribute on top of a variable to limit it with a min and max value.
Watch the full video of 5 Attribute Tips – Unity Tips(Volume #2):https://t.co/bPRzw4Fnj9#UnityTips #GameDev #Unity3D pic.twitter.com/H9EIhJdEXN
#10 Context Menu
You can call a function in the editor (runtime too) and accessing it through the gear icon of the current script by using the “ContextMenu”.
Simply, pass the ContextMenu along with the title on top of the desired function to be called.
[ContextMenu("Say Hello!")]
public void Hello(){
print("Hello!");
}
If you want to use or test a function in the editor, use the “ContextMenu” attribute on top of the method you want to call.
β Omar Balfaqih (@OBalfaqih) February 19, 2019
You’ll find it under the gear iconππ»
Watch the full video of 5 Attribute Tips – Unity Tips(Volume #2):https://t.co/bPRzw4Fnj9#UnityTips #unity3d #gamedev pic.twitter.com/DNqEHMqek6
Share Your Tips!
Which one of the above is your favorite?
If you got other ones, share them in the comments below and on Twitter using #UnityTips.