はじめに
Unityにてファイルの作成・更新を監視した処理を実装していました。
実装にあたり「FileSystemWatcher」クラスを使用していたのですが、Editorでは問題なく動作していたにもかかわらず、Build Runを行うと、動作しないという現象に遭遇して、対処したのでまとめました
TL;DR
Api Compatibility Levelを「.NET 4.x」に変更すればOKです
環境
Unityのバージョン : 2018.4.21f1
現状の把握
-
実行時のログを確認します。ログは以下に出力されています
- C:/Users/【user name】/AppData/LocalLow/【company name】/【product name】
- user name, company name, product nameはプロジェクトの設定に依存するので、各自の環境で読み替えてみてください
- C:/Users/【user name】/AppData/LocalLow/【company name】/【product name】
-
FileSystemWatcherが動作しないとき、以下のようにログに出力されていました
output_log.txt
UnloadTime: 1.114600 ms
NotImplementedException: The method or operation is not implemented.
at System.IO.FileSystemWatcher..ctor () [0x00006] in <0813754e7cfa415ebfedff172c2dd6e0>:0
at (wrapper remoting-invoke-with-check) System.IO.FileSystemWatcher..ctor()
at FileManager.Start () [0x00011] in <e475f99b7ae14fec98b1a675e6f35500>:0
対処法
- File→Build Settingsの順でBuild Settingsを表示します
- 左下のPlayer Settingsボタンをクリックします
- Inspector上にPlayer Settingsが表示されますので、下にスクロールして、Other Player Settingsを展開します
- Api Compatibility Levelを「.NET 4.x」に変更(以下参照)し保存します

まとめ
FileSystemWatcherが動かなくてお困りの方は参考にしてみてください