Repository navigation
Coding Conventions
The coding convention helps create a consistent look for the code. Readers will understand the code more quickly and it facilitates copying, changing and maintaining the code.
In this project, we use several naming conventions. Remember to name your variables accordingly to their function or purpose.
Be aware that
protectedelements are treated the same way aspublicelements
Always use pascal casing ("PascalCasing") when naming classes, structs, properties and methods.
public class MyClass { }
public struct MyStruct { }
public int MyInteger { get; set; }
private void MyMethod() { }Always use camel casing ("camelCasing") when naming private fields and method parameters.
public class MyClass
{
private bool myBoolean;
public void MyMethod(int myParameter) { }
}A good layout uses formatting to emphasize the structure of your code and makes the code easier to read.
- Use four-character indents and save TABs as spaces.
- Write only one statement per line.
- Write only one declaration per line.
- Continuation lines should be indented one time only.
var myObject = new MyClass(0,
40.0f,
"text");- Use parentheses to make clauses in an expression apparent.
if((value1 > value2) && (value1 > value3))
{
// Block of code to execute if the condition is true
}- Place the comment on a separate line, not at the end of a line of code.
- Begin comment text with an uppercase letter.
- Insert one space between the comment delimiter (//) and the comment text.
// This is a comment
// This is another comment
// You can also
// break the lines like this
// if you want!- Don't create formatted bl9ocks of asterisks around comments.
- Ensure all public members have the necessary XML comments providing appropriate descriptions about their behaviour. Use
<summary>:
/// <summary>
/// This method prints a message in the middle of the screen!
/// </summary>
/// <param name="message">The message</param>
/// <param name="color">Message color</param>
/// <param name="bold">Is the message text in bold?</param>
public void PrintMessage(string message, Color color, bool bold)
{
// Implementation
}This section describes practices related to C# language.
- Always use string interpolation to concatenate strings, as shown in the following code snippet:
private string output = $"{name} is {age} year{(age == 1 ? "" : "s")} old.";
- Use implicit typing for local variables when the type of the variable is obvious from the right side of the assignment, or when the precise type is not important.
var name = "Gil Vicente";
var age = 21;
var obj = new Object();- Don't use
varwhen the type is not apparent from the right side of the assignment. Don't assume the type is clear from a method name. A variable type is considered clear if it's anewoperator or an explicit cast.
int amount = MyClass.GetAmount();
MyObject myObject = MyClass.CurrentCache();Use concise syntax when you initialize arrays on the declaration line. In the following example, note that you can't use var instead of string[].
string[] vowels = { "a", "e", "i", "o", "u" };If you use explicit instantiation, you can use var.
var vowels = new string[] { "a", "e", "i", "o", "u" };If you specify an array size, you have to initialize the elements one at a time.
var vowels = new string[5];
vowels[0] = "a";
vowels[1] = "e";
// And so on.- Use one of the concise forms of object instantiation, as shown in the following declarations.
var myObject = new MyClass();MyClass myObject = new();Are the same as their former
MyClass myObject = new MyClass();- Use object initializers to simplify object creation.
var myObject = new MyClass { Name = "Gil Vicente", NIF = 128258258,
Location = "Porto", Age = 21 };Don't do this.
var myObject = new MyClass();
myObject.Name = "Gil Vicente";
myObject.NIF = 128258258;
myObject.Location = "Porto";
myObject.Age = 21;In this section, I'll talk about what convention we work within Unity.
Only make public variables if they need to, or else, make them private. Specify whenever you can, if they have to show up in the Unity Editor or not.
MyClass.cs
public class MyClass
{
[HideInInspector] public MyObject myObject;
public MyObject myReference;
[SerializeField] private MyObject myPrivateReferenceObject;
private MyObject myPrivateObject;
}We like to use Partial Classes as it makes everything cleaner.
Player.cs
public partial class Player : NetworkBehaviour
{
private void Update()
{
...
Move();
...
}
}Player.Movement.cs
public partial class Player
{
private void Move()
{
// Make the player move given the input and the physics.
}
}We like to work with Managers. Managers are used for specific global roles. They're based on the Singleton Utility class so that only one exists at a time.
Here's an example of a Manager.
MyManager.cs
public class MyManager : Singleton<MyManager>
{
public float MyFloat { get; set; }
protected override void Awake()
{
base.Awake();
MyFloat = Time.time;
}
}And this is how you access it.
AnyScript.cs
public void MyMethod()
{
...
// Print the Manager's MyFloat property
Debug.Log(MyManager.Instance.MyFloat);
...
}Getting Started