- Home /
How to open a windows file open or save dialog without crashing Unity?
I want to open a file dialog on windows to open a file with. This is my current code:
var openFileDialog : System.Windows.Forms.OpenFileDialog = new System.Windows.Forms.OpenFileDialog();
openFileDialog.InitialDirectory = Application.dataPath;
openFileDialog.Filter = "text files (*.txt)|*.txt";
openFileDialog.FilterIndex = 2;
openFileDialog.RestoreDirectory = false;
openFileDialog.ShowDialog();
It actually works fine after building my project and kinda okay, when executing it the first time from within Unity, but with the following message in the log:
IsD3D9DeviceLost() || g_D3DInsideScene System.Windows.Forms.XplatUIWin32:Win32GetMessage(MSG&, IntPtr, Int32, Int32) System.Windows.Forms.XplatUIWin32:Win32GetMessage(MSG&, IntPtr, Int32, Int32) System.Windows.Forms.XplatUIWin32:GetMessage(MSG&, IntPtr, Int32, Int32, Boolean) System.Windows.Forms.XplatUIWin32:GetMessage(Object, MSG&, IntPtr, Int32, Int32) System.Windows.Forms.XplatUI:GetMessage(Object, MSG&, IntPtr, Int32, Int32) System.Windows.Forms.Application:RunLoop(Boolean, ApplicationContext) System.Windows.Forms.Form:ShowDialog(IWin32Window) System.Windows.Forms.CommonDialog:ShowDialog(IWin32Window) System.Windows.Forms.CommonDialog:ShowDialog()
The dialog shows up, I can choose my file and open it. If I now leave Unity and am moving a bit through my folder structure using the explorer, I usually get the standart win 7 message that Unity doesnt react anymore. If I dont do this, but open another dialog, I get tons of error messages and in the end the same windows message. The error messages I get then look like this (as popup windows):
Oops: Could not register the window class, win32 error 0
Sometimes the form shows up a second time and everything works as the first time, but then after the third time opening the dialog Unity crashes.
It would be great if anyone could tell me what I am doing wrong and maybe how to solve it :) Thanks.
Why do you use .NET to open a file dialogue? Is is an editor or runtime script?
It is a runtime script. And well, it works at runtime, but not really in the editor ;).
Answer by Nicolaj Schweitz · Apr 20, 2010 at 08:51 AM
Assuming you are using the script for the editor...
Take a look at the EditorUtility class' OpenFilePanel instead of using .NET functionality that might intervene with Unity's processes.
And what if you needed it for an application and not for the editor?
Using the EditorUtility functions works fine in the application as well. Just one line of code for either a file open or file save dialog. This is what I did for c#:
string sOpenPath = EditorUtility.OpenFilePanel("Open Session","","png");
string sSavePath = EditorUtility.SaveFilePanel("Save Session","","","png");
How are you able to compile an application referencing the EditorUtility class? I am unable to build a runtime player that references UnityEditor.
Answer by Ashkan_gc · Apr 11, 2010 at 03:27 PM
make sure to use mono's windows.forms.dll and not the .net one. it should works with both but test it. there is another way to use a C++ dll and call the open dialog and return the result in a dll function call.
Thanks for your answer, but how to use the mono dll? I so far wasnt able to find any information about it while searching through the mono documentation. I know that I could use a C++ dll, but as far as I know, plugins arent supported with the free Unity edition, I currently use.
yes you can use plugins with the pro version. actually i am not sure that mono's windows.forms.dll is different from .net one but you can search inside your mono installation directory and find the assembly. take a look at those parts of mono website about creating windows forms applications.
I seem to be getting the same 'Oops' error, but the dialog box appears to work fine in the editor?
When sent out to a standalone, the dialog box appears without a visible error, but there are no files listed in the box.
As a note, i've got this working pretty well (The error appears in the editor, but everything seems to work in the standalone without any problems.)
I can however crash the editor on command by attempting to hit play with the above code when the BuildProfile switches to the 'WebPlayer' from the standalone mode. This happens somewhere when using my create assetbundles script. someday i hope to have time to isolate!
Answer by Razieln64 · Jul 28, 2015 at 05:04 AM
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.
Your answer
Follow this Question
Related Questions
Windows crash report after closing 0 Answers
Installing Unity 3D 4.3.4 on Windows 8.1 2 Answers
Unity webplayer crashing browser when it is loaded and unloaded into a page using JavaScript 0 Answers
Unity crashes on building project 2 Answers
Crash with 'Standard MaskyMix Scatter (Specular, Surface)' shader 0 Answers