2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【メモ】VSCodeでUnity C#の自動整形環境を自前で用意する(C# FixFormatなし)

Last updated at Posted at 2020-12-23

注意!
.NET 5 の頃の情報です。.NET 6 からは、フォーマッタが本体に同梱されたりしていろいろ仕様が変わりました。

いろんなドキュメントには、C#の自動整形をしたいなら、
「C# FixFormat」という拡張を使えばいいよ!って書いているんだが、
2020年12月24日現在、マーケットプレイス上で非公開になっており、
拡張機能のインストールができないという状態になっている。
詰んだじゃねえか・・・。

しかし俺は、なんとしてもVSCodeでC#の自動整形を使いたい。
というわけで、なんとかしてみたのでここにメモを残す。

なお、筆者は.NETもC#も、ほとんど素人に近いので、
間違いとかあってもご愛敬。
もっと楽な方法あったらコメントで教えてください。

動作環境

  • Windows 10
  • dotnetコマンドが使える環境
    • Chocolatey で手っ取り早く入れるなら cinst -y dotnet-sdk

必要なもの

また、自動整形させたいC#ファイルは、
./Assets/Scripts/**/*.cs に置いておく。

やり方

まず dotnet-format をインストールする。
これはコマンドライン上からC#の自動整形を行うことができる
.NETのツールらしい。

dotnet tool install -g dotnet-format

次にUnityのプロジェクトをVSCodeで開いたら、
クイックメニューで以下のコマンドを打ち込んで、
必要な拡張をインストールする。

ext install emeraldwalk.runonsave

そしたら今度は
クイックメニューからPreferences: Open Settings (JSON) を選び、
開いた設定ファイルに以下の設定データを書き加える。

  "emeraldwalk.runonsave": {
    "commands": [
      {
        "match": "\\\\Assets\\\\Scripts\\\\.*?\\.cs$",
        "cmd": "dotnet format -f ./Assets/Scripts"
      }
    ]
  }

これで完了。

多少のタイムラグはあるものの、./Assets/Scripts/ の中にある
C#ファイルは、保存をトリガーに自動整形が働くようになる。

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?