Unity2018引用System.Windows.Forms.dll发布报错

Unity 2018发布到PC平台,要用到打开选择文件界面,需要引入System.Windows.Forms.dll

打开Visual Studio,选择菜单栏“项目>添加引用”,找到Unity的安装路径,选择\Editor\Data\Mono\lib\mono\2.0\System.Windows.Forms.dll

Unity2018引用System.Windows.Forms.dll发布报错_第1张图片

然后再把System.Windows.Forms.dll导入Unity里面

        System.Windows.Forms.OpenFileDialog od = new System.Windows.Forms.OpenFileDialog();
        od.Title = "请选择图片";
        od.Multiselect = false;
        od.Filter = "图片文件(*.jpg,*.png,*.bmp)|*.jpg;*.png;*.bmp";
        if (od.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            StartCoroutine(GetTexture("file://" + od.FileName));
        }

运行正常,可是发布到PC端编译报错。

The Assembly Mono.WebBrowser is referenced by System.Windows.Forms.But the dll is not allowed to be included or could not be found.

 

解决办法:

1:删除掉工程中的System.Windows.Forms.dll等文件
2:切换 .NET Standard 2.0 ->.NET 4.x
3:Asset 目录下添加 mcs.rsp文件,文件内容 " -r:System.Windows.Forms.dll "

 

官方解释:

https://docs.unity3d.com/Manual/dotnetProfileAssemblies.html

Referencing additional class library assemblies

If a Unity Project needs access to a part of the .NET class library API that is not compiled by default, the Project can inform the C# compiler in Unity. The behavior depends which .NET profile the Project uses.

意思就是引用.NET其他类库组件的时候需要配置来通知编译器编译。

你可能感兴趣的:(CSharp,Unity3d游戏开发)