The jonki

呼ばれて飛び出てじょじょじょじょーんき

[Unity] error CS0234: The type or namespace name `Ports' does not exist in the namespace `System.IO'

UnityでSerialPortを使おうとusing System.IO.Ports;と書いたら下記のエラーが出た。

Assets/Scripts/SerialController.cs(6,33): error CS0234: The type or namespace name `Ports' does not exist in the namespace `System.IO'. Are you missing an assembly reference?


調べてみると、どうやら.Net 2.0 subsetにSerialPortが含まれていないのが原因っぽい。
Using the SerialPort class in Unity 3.1 Free

再現環境
解決方法
  1. [Edit] -> [Project Settings] -> [Player]でProject Settingsを開く
  2. [Settings for PC, MAC & Linux Standalone]の下の方の[Other Settings]を開く
  3. [Optimization]のメニュー内に[Api Compatibility Level]があるので、これを[.Net 2.0 Subset]から[.Net 2.0]に変更する
  4. 一旦エラーが出たScriptをGameObjectから取り除いて、再度アタッチし、プロジェクトをリビルドでエラーが消える


モバイルプラットフォーム用にあえてSubsetを用意してるのかな。モバイルで使うプロジェクトじゃないし、ビルド後の容量もそこまで差がないようなのでこの設定で特に問題なさそう。
Unity Document, Using .NET API 2.0 compatibility level
強火で進め iOS版での.NETの設定による容量の違いAdd Star