When I Want to use OpenFileDialog in unity ,the Script can't Compile

My code :

using System.Windows.Forms; this is unity mono forms
  OpenFileDialog open = new OpenFileDialog();
  open.ShowDialog();

http://answers.unity3d.com/questions/7554/how-to-open-a-windows-file-open-or-save-dialog-without-crashing-unity

What's the error?

i know, this issue was discussed a long time ago but i am struggling with the same problem at the moment. I have implemented the dialog as mentioned above and it works pretty well in the editor. but when i build a .exe, it does not work anymore... tried already 32bit and 64bit builds.. can someone help me please?!!

5 Answers

5

Yet another alternative:

perfect solution. having no decent exposure to the native windows controls is just stupid. gkngkc's package solves this nicely

Copy System.Windows.Forms.dll from Unity_Location\Editor\Data\Mono\lib\mono\2.0 to a Plugins folder in your project.

In your script :

using System.Runtime.InteropServices;

public class myClass{

  [DllImport("user32.dll")]
  private static extern void SaveFileDialog(); //in your case : OpenFileDialog

  private void myMethod()
  {
     System.Windows.Forms.SaveFileDialog sfd = new System.Windows.Forms.SaveFileDialog();

     //your code
  }
}

I dont have that folder anywhere in my computer. What should I do in that case then?

will this work in Mac OS X and Linux?

Worked perfectly for me, thanks! @mmangual_83, "Unity_Location" is usually "C:/Program Files/Unity" and the Plugins folder must be inside your project's Assets folder (you can create it if it doesn't exists). @ivanzinho, Yes, this should work on OSX, Linux and Windows since this DLL uses GTK# which is the same system used to render the unity editor UI.

This works in the Editor, but when I build I get System.Windows.Forms.dll assembly is referenced by user code, but is not supported on StandaloneWindows64 platform. Various failures might follow. UnityEditor.BuildPlayerWindow:BuildPlayerAndRun()

Thank you! I tried this out myself, took a bit of figuring out but after taking bits of this code and taking the time to understand how to use DLLImport everything seems to be working. It took me a while since it works a bit different than usual, I've never had to use it until now. One question though, since you've helped me out with getting the System.Windows libraries working; do you know if it's possible to use System.Windows.Forms.Cursor.Position in Unity, as opposed to using Unity's Pointer Event Handler?

use player setting .net not .net subset

The best thing to do is code a plugin that does this now that all versions of Unity support plugins. You can easily make this multi-platform for all computer OSes by using defines and setting the correct dll/bundle to load.

I’ve just made a plugin for OSX and it works great. Just make sure you build a universal bundle if you plan on making your game run on a 32bit machine.

How are you even able to make a working DLL interpreter for Mac (that fixes the actual problem) if the whole point in trying to do this is to accomplish something in a way Unity can't support.

An alternative way: See my answer for Equivalent of OpenFilePanel without using UnityEditor? - Unity Answers