System.Windows.Forms in Unity

Hello,

I want to open an OpenFileDialog through my Unity game. I added the System.Windows.Forms DLL into a Plugins folder and everything works fine in the editor.

But Unity doesn't allow me to build my application with the System.Windows.Forms DLL.

It seems that there are two different System.Windows.Forms DLLs, an original windows one and a mono one which seems to not have the FileDialog thing included.

Why can't I build my game with the System.Windows.Forms DLL and why has the mono System.Windows.Forms DLL the FileDialog system not included?

best regards

Take a look at http://answers.unity3d.com/questions/56302/when-i-want-to-use-openfiledialog-in-unity-the-scr.html.
Looks like you done most, but did you "use player setting .net not .net subset"

Hope it'll point you in a useful direction.

Thanks for your answer!

I am using .net not .net subset already.

The problem is that OpenFileDialog is for some reason not supported.

Nope, you'll never get it to build. It'll only ever work in the editor. (it's mono implementation of system.windows.forms, who knows what it needs and how it works internally).

Just build your own :) With the new UGUI system, it shouldn't take long.

Take a look at the answer from Viju at http://answers.unity3d.com/questions/770063/equivalent-of-openfilepanel-without-using-unityedi.html#answer-1134105

Might be a good starting point.

But that's the thing. I need the windwos one, not a custom one.

It only works if I build with the mono System.Windows.Forms DLL and after the build I replace that one with the Windows System.Windwos.Forms DLL. Then everything works, but I don't want to do it that way.

This is unsupported and will most likely result in your application not running or simply crashing on any platform other than Windows. Why can't you use a GUI made with Unity to do the same thing?

1 Like

Because in my opinion it's not a good user experience if you have a custom file browser and not the OS one.
BTW: It only has to work on widows.

6 Likes



Why not just replace the DLL after building? From what I remember is it not just a 5 second step? If this is the solution but you have to replace the DLL after building just stick with that. From reading this thread there is no other option unless you build your own.
This thread is basically done anyhow.
Option 1: Replace the System.Windows.Forms DLL in the build with another.
Option 2: Create your own using the GUI System.

Yes you are right, but I don't know, which side effects the DLL replacement has. Maybe someone can tell me where the different between these two DLLs is.

best regards

[quote]
Yes you are right, but I don't know, which side effects the DLL replacement has. Maybe someone can tell me where the different between these two DLLs is.
[/quote]

One is the Microsoft implementation, the other is the Mono implementation. Their interfaces are meant to be the same, so all classes and method names and what not should line up.

I don't think unity cares about the signing on the dll, so it probably won't freak out.

Try replacing it, if it doesn't crash your game, you'll be fine. The only issue I can see happening if you swap it is that the mono runtime checks the assembly domain signature and freaks out because it changed... which like I said, you'll know right away, your game just won't work.

And if that happens, will have to find another route of doing it.

[quote]
Nope, you'll never get it to build. It'll only ever work in the editor. (it's mono implementation of system.windows.forms, who knows what it needs and how it works internally).
[/quote]

Well, considering that mono is open source, anyone who looks into the source code knows what it needs and how it works:
https://github.com/mono/winforms

...

Back @OP - I'm also willing to bet the reason it's missing from the mono implementation of WinForms is because we have an older version of it. Try using a newer version of the mono one if the previous option doesn't work.

Hi, I figured it out and it works so far so good. Building throws an unsupported warning, but seem that it does not matter. What I did:
- Set the library from .NET 2.0 subset to .NET 2.0.
- Copy the System.Windows.Forms and System.Drawing to the plugins folder. Note that you must use the Mono ones (can be found in somewhere deep in unity installation folder). .NET ones do not work (if I remember correctly, they worked in the editor but could not build).
- Then I use the Visual Studio to make the desired .NET code for me and then just copy it to my MonoBehaviour script.

If you are experiencing issues using System.Windows.Forms.dll
https://feedback.unity3d.com/suggestions/do-not-remove-references-to-non-net-core-dlls-and-let-us-decide-if-we-want-to-use-them
Please vote on this!

Try changing the Api compatibility Level from .NET Standard 2.0 to .NET 4X.
This Solution worked from me
I'm using Unity 2018.3.5f1.
Check the attached image for reference
4608499--430732--browse.JPG

1 Like

changing to net 4x didn't work for me building with il2cpp

1 Like

IL2CPP in Unity 2020.2.1f compiles fine with WinForms.dll taken from
C:\Windows\Microsoft.NET\Framework64\v4.x.xx

and placed inside Plugins folder.

5 Likes

thanks, GeorgeAdamon. it works for me.
Initially I copied dll file from unity/mono folders, but it didn't work.

Since Unity already has a System.Windows.Forms.dll in its environment, we can just use that dll by a csc.rsp file:
1. Find your .asmdef file location
2. In that directory, create a csc.rsp file, and edit it with notepad or something else
3. Type "-r:System.Windows.Forms.dll" in that file and save it.

-r:System.Windows.Forms.dll
  1. Back to Unity, refresh and recompile
  2. Use System.Windows.Forms.xxx in your code. :)

Updating to Unity 2021.3.6 allows you to import System.Windows.Froms.dll from the UnityStandaloneFileBrowser

There were some errors with something called Unity.PlasticSCM.Editor.dll
but it builds fine and there were no further issues

1 Like