Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Access level modifiers should be explicitly defined for classes, methods and member variables. This includes defining private even though C# will implicitly add it.
Use the least accessible access modifier, except for public member that is expected to be used by other classes in the future.
Prefer single declaration per line.
BAD:
GOOD:
Spacing is especially important to make code more readable.
Indentation should be done using tabs — never spaces .
Blocks
Indentation for blocks uses tabs for optimal readability:
BAD:
GOOD:
Line Wraps
Indentation tab for line wraps should use 4 spaces (not the default 8):
BAD:
GOOD:
Lines should be no longer than 100 characters long.
There should be just one or two blank lines between methods to aid in visual clarity and organization. Whitespace within methods should separate functionality, but having too many sections in a method often means you should refactor into several methods.
All braces get their own line as it is a C# convention:
BAD:
GOOD:
Conditional statements are preferred to be enclosed with braces, irrespective of the number of lines required. Some cases can be pardoned.
BAD:
GOOD:
Exactly one class, struct, enum, or interface per source file, although inner classes are encouraged where scoping appropriate.
Switch-statements come with default
case by default (heh). If the default
case is never reached, be sure to remove it.
If the default
case is an unexpected value, it is encouraged to log and return an error
BAD
GOOD
BETTER
Use US English spelling.
BAD:
GOOD:
The exception here is MonoBehaviour
as that's what the class is actually called.
Whenever someone add a script implementing NetworkBehaviour, they have access to the following attributes, indicating if a given method should be server only, client only, or both :
[Server] for server only code.
[Client] for client only code.
[ServerOrClient] for both.
Make sure no methods goes without an attribute in a script inheriting NetworkBehaviour. The goal is to increase readability and to generate warnings or exceptions when clients or server are attempting to call something they should not.
Methods with [ServerOrClient] attributes should stay uncommon, they make debugging harder. If possible, try to refactor the code to avoid them.
This section includes some rules of thumb for design patterns and code structure
Avoid throwing exceptions. Instead log and error. Methods returning values should return null in addition to logging an error
BAD:
GOOD:
Don't use Find or in other ways refer to GameObjects by name or child index when possible. Reference by reference is less error prone and more flexible. Expect people to set the fields in the inspector and log warnings if they don't.
BAD:
Prefer RequireComponent and GetComponent over AddComponent. Having the components in the inspector let's us edit them. AddComponent limits us.
BAD:
GOOD:
Properties can be used for access control to fields, and when using backing fields they can be private and let us change them in the inspector. Consider when a fields should be public and prefer properties with backing fields.
Sometimes it's just nice to see them for debugging, even if we don't change them, so consider making more of your private fields visible.
OKAY:
BETTER:
A class must respect the following order, from top to bottom
Events and delegates declaration.
Private enums declaration.
Private internal classes.
Member variables.
Properties.
Methods.
BAD:
GOOD: