Repository navigation
Code Conventions
Unity C# code conventions.
All code should be in English. Always try to adhere to the Single Responsibility Principle. And make sure the code is DRY (Don't Repeat Yourself)
Also known as camel caps, camel case is the practice of writing phrases without spaces or punctuation, separating words with a single capitalized letter. The very first letter is in lowercase.
We write all variable names in camel case. For example:
examplePlayerController
maxHealthPoints
endOfFile
Pascal case is a variation of camel case, where the initial letter is capitalized. Use this for class and function names.
TakeDamage
OnPlayerMove
EndOfFile
Captitalized snake casing is used for constant names
NONE
FIRST_CONSTANT
SECOND_CONSTANT
Namespace names should be written in Pascal Case. Every Class, ScriptableObject and Struct should be inside of a namespace.
namespace ExampleNamespace
{
public class ExampleClass : MonoBehaviour
{
}
}Consider these rules for your variables and fields:
Use nouns for variable names: Variable names should be clear and descriptive because they represent a specific thing or state. Use a noun when naming them except when the variable is of the type bool (see below).
Prefix Booleans with a verb: These variables indicate a true or false value. Often they are the answer to a question, such as: Is the player running? Is the game over?
- Prefix them with a verb to make their meaning more apparent. Often this is paired with a description or condition, e.g., isDead, isWalking, hasDamageMultiplier, etc.
Use meaningful names. Don’t abbreviate (unless it’s math): Your variable names will reveal their intent. Choose names that are easy to pronounce and search for.
- While single-letter variables are fine for loops and math expressions, don’t abbreviate in other situations. Clarity is more important than any time saved from omitting a few vowels.
Avoid too many prefixes or special encoding: The variable or function name should be clear enough and easily readable. Only prefixes based on the access modifier should be used (see below).
Specify access level modifiers consistently: If you leave off the access modifier, the compiler will assume the access level should be private. This works, but always use an access modifier for clarity. Variables should be private by default.
- Only use public if absolutely necessary. Only use [SerializeField] when the variable needs to be ajustable in the editor
- Remember that you’ll need to use protected if you want this in a subclass later.
//public variables should follow the default conventions
public int publicExample;
//private variables should start with an _underscore
private int _privateExample;
//Serialized variables should follow public example rules
[SerializeField] private int serializedExample;
//protected variables should start with p_
protected int p_protectedExample;
//internal variables should start with i_
internal int i_internalExample;Only use [SerializeField] when the variable needs to be ajustable in the editor
Headers should be used if there are a large amount of editable values in the editor. do not use headers if it is not needed as this will bloat code and make it harder to read. A header name should clearly describe what the group of variables is for.
Only use tooltips when a variable name is not enough to know what exactly the value does. Make sure a tooltip is very clear in its description.
Follow these standard rules when naming your classes and interfaces:
- Use pascal case nouns for class names: This will keep your classes organized.
- If you have a MonoBehaviour in a file, the source file name must match: You might have other internal classes in the file, but only one MonoBehaviour should exist per file.
// EXAMPLE: Class formatting
public class ExampleClass : MonoBehaviour
{
public int PublicField;
public static int MyStaticField;
private int packagePrivate;
private int myPrivate;
private static int myPrivate;
protected int myProtected;
public void DoSomething()
{
.l
}
}Class members should be grouped into sections:
- Constant Fields
- Static Fields
- Fields
- Constructors
- Properties
- Events / Delegates
- LifeCycle Functions (Awake, OnEnable, OnDisable, OnDestroy, IEnumerator)
- Public Functions
- Protected Functions
- Private Functions
- Nested types
Within each of these groups order by access:
- public
- Serialized Fields
- internal
- protected
- private
Prefix interface names with a capital “I”: Follow this with an adjective that describes the functionality.
// EXAMPLE: Interfaces
public interface IKillable
{
void Kill();
}
public interface IDamageable<T>
{
void Damage(T damageTaken);
}In C#, every executed instruction is performed in the context of a function.
Functions perform actions, so apply these rules to name them accordingly:
- Start the name with a verb: Add context if necessary (e.g., GetDirection, FindTarget, etc.)
- Use camel case for parameters: Format parameters passed into the functions like local variables.
- Functions returning bool should ask questions: Much like Boolean variables themselves, prefix functions with a verb if they return a true-false condition. This phrases them in the form of a question (e.g., IsGameOver, HasStartedTurn).
// EXAMPLE: Methods start with a verb.
public void SetInitialPosition(float x, float y, float z)
{
transform.position = new Vector3(x, y, z);
}
// EXAMPLE: Methods ask a question when they return bool.
public bool IsNewPosition(Vector3 currentPosition)
{
return (transform.position == newPosition);
}Enum names are written in Pascal Case, the constants are written in full capitals with Snake Casing. The default constant should always be at the top
Enum Example
{
EXAMPLE_CONSTANT
ANOTHER_EXAMPLE
}Struct names are written in Pascal Case. Everything inside follows the usual code conventions
public struct ExampleStruct
{
public example x;
public example y;
}Scriptable objects only hold data and settings, the name needs to clearly reflect what it is for. Always include the CreateAssetMenu.
[CreateAssetMenu(fileName = "NewProjectileData", menuName = "Projectile Data")]
Public sealed class Projectile : ScriptableObject
{
private int _currentSpeed;
public int baseMoveSpeed;
public void ResetSpeed() => _currentSpeed = baseMoveSpeed;
}