Search Unity

System.Windows.Forms in Unity

Discussion in 'Scripting' started by S_P_S, Jun 29, 2016.

  1. S_P_S

    S_P_S

    Joined:
    Feb 25, 2015
    Posts:
    91
    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
     
  2. Henning_Justare

    Henning_Justare

    Joined:
    Nov 10, 2014
    Posts:
    21
  3. S_P_S

    S_P_S

    Joined:
    Feb 25, 2015
    Posts:
    91
    Thanks for your answer!

    I am using .net not .net subset already.

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

    Henning_Justare

    Joined:
    Nov 10, 2014
    Posts:
    21
  5. S_P_S

    S_P_S

    Joined:
    Feb 25, 2015
    Posts:
    91
    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.
     
  6. jimroberts

    jimroberts

    Joined:
    Sep 4, 2014
    Posts:
    560
    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?
     
    Henning_Justare likes this.
  7. S_P_S

    S_P_S

    Joined:
    Feb 25, 2015
    Posts:
    91
    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.
     
  8. TaleOf4Gamers

    TaleOf4Gamers

    Joined:
    Nov 15, 2013
    Posts:
    825
    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.
     
  9. S_P_S

    S_P_S

    Joined:
    Feb 25, 2015
    Posts:
    91
    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
     
  10. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,537
    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.

    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.
     
  11. Polci

    Polci

    Joined:
    Apr 8, 2015
    Posts:
    1
    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.
     
    Last edited: Sep 10, 2016
  12. Sir-Thorgeir

    Sir-Thorgeir

    Joined:
    Jan 17, 2013
    Posts:
    16
  13. SHAILAN

    SHAILAN

    Joined:
    Jun 3, 2019
    Posts:
    1
    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
    browse.JPG
     
    unknownsk likes this.
  14. Tarrag

    Tarrag

    Joined:
    Nov 7, 2016
    Posts:
    215
    changing to net 4x didn't work for me building with il2cpp
     
    GeorgeAdamon likes this.
  15. GeorgeAdamon

    GeorgeAdamon

    Joined:
    May 31, 2017
    Posts:
    48
    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.
     
  16. TommyDinh

    TommyDinh

    Joined:
    Aug 16, 2021
    Posts:
    1
    thanks, GeorgeAdamon. it works for me.
    Initially I copied dll file from unity/mono folders, but it didn't work.
     
  17. dbdenny

    dbdenny

    Joined:
    Mar 13, 2020
    Posts:
    12
    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.
    Code (CSharp):
    1. -r:System.Windows.Forms.dll
    4. Back to Unity, refresh and recompile
    5. Use System.Windows.Forms.xxx in your code.
    :)
     
  18. Atangames

    Atangames

    Joined:
    Sep 16, 2020
    Posts:
    22
    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
     
    Laiken likes this.