Skip to content

Code Style

Andreas Stange edited this page Jun 6, 2022 · 1 revision
  • Indent using 4 spaces

  • All curly braces get their own line

  • Use explicitly typed variables (no var keyword)

  • Skip the constructor name when it is equal to the expected type name. For example, use Bla bla = new(), don't use Bla bla = new Bla().

  • PascalCase: NameSpace, Class, IInterface, EEnum, Method, Property, Constant, Event

  • camelCase: parameters, all fields

  • Prefixes: E for enums, I for interfaces, but none for private or static fields

  • Suffixes:

    • Scene: scenes files
    • Ui: UXML files
    • Manager: singleton classes
    • Control: often used as suffix for a code counterpart of something.
    • Examples:
      • MainScene.unity has a corresponding MainSceneUi.uxml and MainSceneControl.cs.
      • There is a single SettingsManager instance available in all scenes.
  • No acronyms, except for the above mentioned prefixes and common abbreviations, such as Http or Xml

  • In code, acronyms should be treated as words, for example: XmlHttpRequest

  • Use private where possible

  • Avoid static where possible

    • static fields have to be reset explicitly because Domain Reload is disabled for UltraStar Play.

Example:

public class MySceneControl 
{
    public int publicField;
    int packagePrivate;
    private int myPrivate;
    protected int myProtected;
    
    public string MyProperty { get; private set; }

    public event Action<int> ValueChanged;

    public MySceneControl()
    {
        List<string> stringList = new();
    }
}

public enum EDay
{
    Today, Tomorrow
}

public interface IXmlReader {
    void ReadXml(string filePath);
}

Configuration

The code style has been configured in a file .editorconfig, which is supported by many IDEs (Visual Studio Code, Visual Studio, Rider, etc.).

Configure your IDE accordingly and follow above conventions.

Clone this wiki locally