C# Code Style Guide For Game Creator
Unified code for the Game Creator framework and Unity
The documentation has moved to: https://mitschmr-studios.io/documentation/codestyle/codestyle.html
This version will no longer be updated and maintained.
Introduction
Seeing how many programming languages there are and how many companies use their own coding conventions, it is favorable for the developers to have a certain standard they can refer to. The same goes for the Unity assetstore, where a developer of an asset has written code that may be or may not be readable. This code style guide is designed to give you an overview of how code may be structured within the Game Creator eco system. It is based on these two coding style guides:
The target Unity version for this code style guide is Unity 2020.3 LTS with support for the C# 8 language features. Reference: https://docs.unity3d.com/2020.3/Documentation/Manual/CSharpCompiler.html
Organization
Source Files
One class per file (except classes in a class)
The class name must match the file name (e.g. class name is
Test
then the file name isTest.cs
)
Ordering
The structure of a script is as follows:
using
statementsnamespace
statementsClass
andInterface
declarations
Layout
Indentation is four spaces long (equals one tab)
Only one statement per line
Only one declaration per line
If continuation lines are not indented automatically, indent them one tab stop (four spaces)
Add at least one blank line between method definitions and property definitions
Wrapping lines
When an expression does not fit on a single line, follow these breaking rules:
Break after an operator
Break after a comma
Indent once after a break (see 4th point in Layout)
Comments
Place the comment on a separate line, not at the end of a line of code
Begin comment text with an uppercase letter
End comment text with a period
Insert one space between the comment delimiter (//) and the comment text, as shown in the following example:
Language
Declaration security
In order to not need to change a script of a module of Game Creator, the fields etc. have the protected
security flag as default, so they can be accessed and changed by scripts that derive of the other.
Method declaration
Methods are generally written with the virtual
keyword, so they can be overridden by other scripts.
Naming
Strings
String interpolation should be used to concatenate string:
Local Variables
Implicit typing for local variables may be used when the type of the variable is obvious from the right side of the assignment or when the precise type is not important
var should not be used when the type is not apparent from the right side of the assignment
Do not rely on the variable name to specify the type of the variable, it might not be correct
Arrays
Use the concise syntax when you initialize arrays on the declaration line:
Conditional logical operators
Use && as well as || for comparisons in order to avoid exceptions and increased performance due to skipped unnecessary comparisons.
Null propagation
Unity overrides the null comparison operator for Unity objects and therefore the C# null propagation feature is incompatible.
Last updated