This document defines a standard for code structure and format as well as patterns for Unity projects.
Favor clarity and readability over optimization. Optimization should only happen once the need is confirmed.
The C# XMLdoc standard is used as it is readable by various IDEs as well as documentation generators. Use of the C# XML Documentation Comments plugin is recommended
Every class must have a summary. E.g.:
/// <summary>
/// This class handles all character movement
/// </summary>Methods must also use XML documentation as long as it is not self-explanatory. E.g.:
/// <summary>
/// Retrieves the player with the provided uid (incremental database
/// counter) from …
/// </summary>
/// <returns>The player or null if not found</returns>
/// <param name="databaseUid">Internal unique id in the database.</param>
public Player GetPlayerByDatabaseUid(long databaseUid)- nested types (enums, classes, etc.).
- static
- fields
- properties
- methods
- fields
- properties
- constructor
- Unity messages (Awake, Start, etc.)
- methods
Within each group the public elements should be at the top and the private ones at the bottom.
- Classes, methods and public fields/properties are named in pascal case (
MyAwesomeClass) - Interfaces are prefixed with an I (
IMyAwesomeInterface) - Private fields, properties and local variables are named in camel case (
myPrivateMethod) - Do not use Hungarian notation or similar prefixes (e.g.
bFlagorm_length) - Enums should be named in plural form if possible (e.g.
PlayerTypes,Directions)
Fields should always be accessed via this
this.counter;
- Each curly bracket should have its own line.
- Brackets are required to open and end on the same level as their counterpart:
private bool MyFunction(int a)
{
if (a == 1)
{
Debug.Log(“Test”);
return true;
}
else
{
MyMethod();
return false;
}
}- If statements must use the same bracket style as methods.
- If the statements are short they should be written in one line:
if(a == b) return true;- Do not use line breaks without brackets like this:
if(a == b)
return true;- Use explicit null checks:
if(temp == null)- Do not use implicit null checks:
if(temp)- Awake
- Initialize the component
- Find all required components (GetComponent)
- Avoid calling methods or changing state of other components
- Start
- Interact with other components here
- OnEnable
- Register events and deleagates
- OnDisable
- Deregister events and delegates