Auto-Formatting C# in VSCode for Unity 3D

Flutter in VSCode has spoiled me: Now I want to use auto-formatting everywhere!

Luckily you can also enable this for Unity 3D & VSCode! Here is how:

  • Install VSCode
  • Install the C# Plugin
  • Set the formatter for C# to “ms-vscode.csharp”. The entry in your settings should look like this:
  • You can see how this file would look in the settings-file I use, here
  • Create a file called “omnisharp.json”, and add these lines:

{
“FormattingOptions”: {
“newLine”: “\n”,
“useTabs”: false,
“tabSize”: 2,
“indentationSize”: 2,

“NewLinesForBracesInTypes”: false,
“NewLinesForBracesInMethods”: false,
“NewLinesForBracesInProperties”: false,
“NewLinesForBracesInAccessors”: false,
“NewLinesForBracesInAnonymousMethods”: false,
“NewLinesForBracesInControlBlocks”: false,
“NewLinesForBracesInAnonymousTypes”: false,
“NewLinesForBracesInObjectCollectionArrayInitializers”: false,
“NewLinesForBracesInLambdaExpressionBody”: false,

“NewLineForElse”: false,
“NewLineForCatch”: false,
“NewLineForFinally”: false,
“NewLineForMembersInObjectInit”: false,
“NewLineForMembersInAnonymousTypes”: false,
“NewLineForClausesInQuery”: false
}
}

 

  • Here is also where you can set your auto-formatting preferences (tab-size, indentations, etc)
  • Copy this file into your Unity-Source directory. It should be next to your “Assets” folder, and the assembly-files VSCode auto-creates. Don’t forget to add it to source-control!

  • Open your Unity-Scripts from Unity in VSCode
  • Wait until the project has been properly loaded.
  • Hit Alt-Shift-F

And Voilá! Your entire code just auto-formatted itself, and saved you a lot of headaches. I was able to update 8-year-old files into my current preferred formatting with just a single key-combination!

-Matthias (@matthias_code)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s