Use Unity Attributes to Optimize Your Game Design Workflow

Save yourself some time in your next game dev project and use attributes to organize and assist!

Unity Logo with [Attributes]
Unity Logo with [Attributes]
Let’s learn about C# Attributes in Unity!

1. [HelpURL]

Example of Reference docs for Unity via HelpURL
Example of Reference docs for Unity via HelpURL
Where to find the Reference document link created by [HelpURL]
[HelpURL("https://cjames1.medium.com/")]public class Enemy: MonoBehaviour{}

2. [DisallowMultipleComponents]

[DisallowMultipleComponents]public class Enemy: MonoBehaviour{}

3. [RequireComponent]

[RequireComponent(typeof(Entity))]public class Enemy: MonoBehaviour{}

4. [Header]

Example Headers for variables in Unity Inspector
Example Headers for variables in Unity Inspector
Unity Headers attribute in action (Stats and Info)
public class Enemy: MonoBehaviour{
[Header("Stats")]
public int maxExp;
public int health;
public int maxHealth;
}

5. [Range]

public class Enemy: MonoBehaviour{
[Range(0, 100)]
public int maxExp;
public int health;
public int maxHealth;
}

6. [Tooltip]

public class Enemy: MonoBehaviour{
[Tooltip("Defines the maximum experience for enemies")]
public int maxExp;
public int health;
public int maxHealth;
}

7. [HideInInspector]

public class Enemy: MonoBehaviour{
public int maxExp;
[HideInInspector]
public int health;
public int maxHealth;
}

8. [SerializeField]

public class Enemy: MonoBehaviour{
public int maxExp;
public int health;
[SerializeField]
private int maxHealth;
}

9. [Space]

public class Enemy: MonoBehaviour{
public int maxExp;
public int health;
private int maxHealth;
[Space] public long id;
public string enemyName;
public string longDescriptionText;
}

10. [TextArea]

Example TextArea attribute expanding the input field for text in Unity Inspector
Example TextArea attribute expanding the input field for text in Unity Inspector
[TextArea] allows for the larger text input field seen for “Long Description Text”
public class Enemy: MonoBehaviour{
public long id;
public string enemyName;
[TextArea]
public string longDescriptionText;
}

11. [UnityEditor.MenuItem]

public class Enemy: MonoBehaviour{  [UnityEditor.MenuItem("Utils/Enemies/Appear All")]
public static void AppearAll(){
var enemyList = FindObjectsOfType<Enemy>();
foreach(GameObject e in enemyList){
e.GetComponent<SpriteRenderer>().color = Color.white;
}
}
}
Feel free to use this guide as a refresher on attributes in Unity C#!

Conclusion

Image for post
Image for post
Game development and design is a fun and rewarding pursuit! Photo by Scott Graham on Unsplash

Written by

Freelance game designer / developer. Full stack developer. Board game geek and cat tamer.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store