Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prettier not formatting Unity C# Code - "Cannot format Movement.cs"

Tags:

when I create a simple .cs file in VS Code in any folder, without a project setup or anything, it will format correctly using the Prettier extension (onSave).

But I cannot get the autoformatting to work when I open a C# file from unity. Or in a Unity Project.

In the left bottom bar I get an error: "Extension Prettier - Code Formatter cannot format Assets\Movement.cs" I've checked the developer console and there is no error or anything.

like image 935
HideAndSeek Avatar asked Mar 23 '20 16:03

HideAndSeek


People also ask

Does prettier work for C#?

Prettier C# adds C# support to the Prettier code formatter. Like Prettier, it is opinionated and restricts style options to a minimum. It runs where Prettier runs, including CI and pre-commit hooks.


1 Answers

That's because Prettier can't format C#.

To format C# code:

  1. Install C# VS Code extension by Microsoft

  2. Add these to your VS Code settings.json

"editor.formatOnSave": true,
"[csharp]": {
  "editor.defaultFormatter": "ms-dotnettools.csharp"
}
  1. Create a omnisharp.json file at your unity project directory and set the formatting options (see options)
{
  "FormattingOptions": {
    "organizeImports": false,
    "UseTabs": false,
    "TabSize": 4,
    "IndentationSize": 4
  }
}
like image 55
Matt Peng Avatar answered Sep 22 '22 11:09

Matt Peng