在 Unity3D 的 System.Drawing 中找不到位图

Bitmap not found in System.Drawing in Unity3D

也许只是我的疏忽,但我无法理解这一点:

但是,如果我尝试使用位图,则会出现以下错误:

Error CS0246 The type or namespace name 'Bitmap' could not be found (are you missing a using directive or an assembly reference?)

VS19-IntelliSense 建议安装包 System.Drawing.Common,但是如果我这样做,我会收到另一个错误,指出:

The type name 'Bitmap' could not be found in the namespace 'System.Drawing'. This type has been forwarded to assembly 'System.Drawing.

如何解决这个问题以实际使用 Bitmap class?

System.Drawing.dllC:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0 复制到您的 Unity 项目文件夹中。我把我的放在 Assets/Plugins/System.Drawing.dll.

此版本将包含 Bitmap class。您仍然会在编辑器中遇到错误,但是一切都会正常编译并且您应该能够在 Visual Studio.

中使用位图

使用此 dll 中的某些类型(例如 ColorRectangleF)时,您现在可能会遇到的一个问题是 Unity 会混淆 4.0 和 2.0 版本的 dll 文件。我发现解决此问题的唯一解决方案是在 Edit > Project Settings... > Player 下将 Api compatibility Level.NET Standard 2.0 更改为 4.0。 .

如果你选择不这样做,你会发现C:/Program Files/Unity/Hub/Editor/2020.2.0f1/Editor/Data/NetStandard/ref/2.0.0/netstandard.dll中有System.Drawing。删除此 dll 将导致 Unity 报错,直到您恢复它。

有建议修改 C:\Program Files\Unity\Hub\Editor20.2.0f1\Editor\Data\NetStandard\compat.0.0\shims\netfx\System.Drawing.dllC:\Program Files\Unity\Hub\Editor20.2.0f1\Editor\Data\MonoBleedingEdge\lib\mono.5\System.Drawing.dll 的解决方案,但我不建议这样做,因为它会影响使用相同 Unity 版本的其他项目。