- Home /
System.IO.Ports with .Net Standard 2.0?
I am developing a managed plug-in to interface with a hardware device and I need to use System.IO.Ports. The plug-in is developed outside Unity, as a Class Library (.Net Standard 2.0), compiled as a dll and placed in Assets/Plugins. The System.IO.Ports is installed as a Nuget package.
I am using Unity 2020.2.x
I am copying the System.IO.Ports dll from my VS solution into the Plugins folder in Unity, but when I run the code it fails with an exception:
System.PlatformNotSupportedException: System.IO.Ports is currently only supported on Windows.
at System.IO.Ports.SerialPort.GetPortNames () [0x00005] in <5fbfe4b89abd41dd9fce05171c2bebc8>:0
The project is actually set up as Windows Standalone, so the error message is misleading.
If I don't copy the dll across, the system won't find the System.IO.Ports namespace and complain about missing assemblies.
System.IO.Ports is available in Unity if we set the project to .Net 4.x, but unfortunately, I cannot use .Net 4.x as I have some dependencies on APIs that are only present in .Net Standard 2.0.
Is it possible to use Nuget libraries in Unity at all? Is this only an issue with this particular package? Is there a workaround to get System.IO.Ports working with .Net Standard 2.0?
Answer by aeldron · Feb 19, 2021 at 05:59 PM
Update: it seems that it is possible to create a mixed VS solution with .Net Standard 2.0 and .Net Framework 4.6.1.
I set my main project to Framework 4.6.1 and linked my library projects using Standard 2.0 as dependencies.
I deleted the Systems.IO.Ports Nuget since it is already included in .Net 4.6.1.
I was then able to build my dll and place it into Unity/Plugins, and set Unity to .Net 4.6.1 in Player Settings. My other dlls in .Net Standard 2.0 seem to still work just fine.