Unity allows developers to use property attributes to modify how a class instance looks within the inspector. Here are some of them and how they work.
[Header("Some Header")]
Header allows you to title a set of variables so if you had a string for the characters name and a string for their description you can place a header attribute above them to signal to anyone that those variables are specifically for a character's description, then use another header attribute for the character's movement stats.
[Tooltip("Some Tip")]
Tooltip allows you to display a tip within the inspector to help explain any variable viewed in the inspector. This is especially useful if the naming of the variable is confusing or could possibly be confused with another variable.
[ContextMenu("Some Menu Name")]
Context Menu allows you to call a function defined within the class from the inspector and can be used to check the functionality of a function without going into play mode in the editor. This attribute is placed above the function that would be called. An example of this would be a TakeDamage() function that in game would be called if the player/character gets hit. Adding a context menu attribute allows that TakeDamage function to be visible in the inspector and allows you to check that the function works as expected due to being able to see the characters health decreasing when called.
[Multiline]
The multiline attribute modifies the text field of a string to be a text area instead, providing more room to write and as the name suggests use multiple lines. This attribute is useful for things such as character/level descriptions.
[Range(min, max)]
The range attribute creates a slider for the variable attached to and requires a min value and a max value. An example use of this would be for a damage per second variable, where you can adjust that value using the slider instead of typing a value in.
[ContextMenuItem("Some Menu Item Name", "Some Function Name")]
Context Menu Item works similar to the context menu attribute but is specific to a variable aka the one below said attribute. This will then call a specified function when clicked on within the inspector. An example of this would be a characters health, which has a context menu item that calls the TakeDamage function from earlier. Another could be added that can then be used to call a GainHealth function, which gives the character more health.
[CreateAssetMenu(fileName = "Some File Name", menuName = "Some Menu Name", order = Some Number]
Create Asset Menu is particularly useful when creating scriptable object classes. This attribute allows you to create a scriptable object file within the project view. This attribute goes above the class definition.
For Example:
[CreateAssetMenu(fileName = "Gun Stats", menuName = "ScriptableObject/Guns/Stats", order = 1]
public class GunStats : ScriptableObject
{
public float reloadRate;
}
[Space]
The space attribute added a gap between two variables being displayed within the unity inspector.
No comments:
Post a Comment