Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by AdamBlaszczyk · Oct 09, 2018 at 08:52 PM · messageexecutableexemessagesmessagebox

Message Boxes and running .exe files

I'm creating a game and i want to make it crash for plot purposes. I thought that easier than crashing the game is to simulate the crash (closing the game and creating message box that looks like error message). And here is first problem. Windows MessageBox is located in System.Windows.Forms, but I can't add it to script by "using System.Windows.Forms; ". I looked for solution and 99% of them suggested to use unity message boxes or to create my own ones, but I really want it to be Windows MessageBox. So first question: is this possible to add that library somehow? My other idea to solve the problem was to write program that shows MessageBox and use its executable file. Something like that:

     Process pr = new Process();
     pr.StartInfo.FileName = System.IO.Directory.GetCurrentDirectory() + "/Assets/Executables/CrashMessageBox.exe";
     pr.Start();

All is OK as long as i work in editor, but it won't work after I build my game, so here is the second problem. Is there any option to add this .exe file to the project in a way that allows to run it later?

Sorry for my english and thanks for help in advance.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Zaeran · Oct 12, 2018 at 03:54 PM

You should be able to add the .exe to the StreamingAssets folder to use it at runtime.

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by dmr32 · Jun 04, 2019 at 06:03 PM

@AdamBlaszczyk If it's just Windows you're wanting it for, don't forget that you don't have to use System.Windows.Forms - you can use PInvoke to bypass .Net and call the Win32 dlls directly. For example:

 #if UNITY_STANDALONE_WIN
 using System.Runtime.InteropServices;
 public static class Win32MessageBox
 {
     public enum ButtonID
     {
         Ok = 1,
         Cancel = 2,
         Abort = 3,
         Retry = 4,
         Ignore = 5,
         Yes = 6,
         No = 7,
         TryAgain = 10,
         Continue = 11,
     }
 
     [System.Flags]
     public enum Flags : uint
     {
         // button selection:
         MB_ABORTRETRYIGNORE = 0x00000002,   // Abort, Retry, Ignore
         MB_CANCELTRYCONTINUE = 0x00000006,  // Cancel, Try Again, Continue. Use this message box type instead of MB_ABORTRETRYIGNORE.  
         MB_HELP = 0x00004000,               // Help
         MB_OK = 0x00000000,                 // OK [default]
         MB_OKCANCEL = 0x00000001,           // OK, Cancel
         MB_RETRYCANCEL = 0x00000005,        // Retry, Cancel
         MB_YESNO = 0x00000004,              // Yes, No
         MB_YESNOCANCEL = 0x00000003,        // Yes, No, Cancel
 
         // icon selection
         MB_ICONEXCLAMATION = 0x00000030,    // An exclamation-point icon appears in the message box.  
         MB_ICONWARNING = 0x00000030,        // An exclamation-point icon appears in the message box.  
         MB_ICONINFORMATION = 0x00000040,    // An icon consisting of a lowercase letter i in a circle appears in the message box.  
         MB_ICONASTERISK = 0x00000040,       // An icon consisting of a lowercase letter i in a circle appears in the message box.  
         //MB_ICONQUESTION = 0x00000020,       // A question-mark icon appears in the message box. Deprecated
         MB_ICONSTOP = 0x00000010,           // A stop-sign icon appears in the message box.  
         MB_ICONERROR = 0x00000010,          // A stop-sign icon appears in the message box.  
         MB_ICONHAND = 0x00000010,           // A stop-sign icon appears in the message box.  
   
         // default button
         MB_DEFBUTTON1 = 0x00000000,         // first button [default]
         MB_DEFBUTTON2 = 0x00000100,         // second button
         MB_DEFBUTTON3 = 0x00000200,         // third button
         MB_DEFBUTTON4 = 0x00000300,         // fourth button
 
         // modal control (since hWnd=null, these don't provide much)
         MB_APPLMODAL = 0x00000000,          // default
         MB_SYSTEMMODAL = 0x00001000,        // Same as MB_APPLMODAL except that the message box has the WS_EX_TOPMOST style
         MB_TASKMODAL = 0x00002000,          // Maybe the best option
 
         // other flags:
         MB_DEFAULT_DESKTOP_ONLY = 0x00020000,
         MB_RIGHT = 0x00080000,              // text is right justified
         MB_RTLREADING = 0x00100000,         // right-to-left on Hebrew/Arabic
         MB_SETFOREGROUND = 0x00010000,      // message box becomes foreground window
         MB_TOPMOST = 0x00040000,            // adds WS_EX_TOPMOST window style
         MB_SERVICE_NOTIFICATION = 0x00200000,
     }
 
     [DllImport("user32")]
     private static extern int MessageBoxW([MarshalAs(UnmanagedType.LPWStr)]string dummyHWnd, [MarshalAs(UnmanagedType.LPWStr)]string text, [MarshalAs(UnmanagedType.LPWStr)]string caption, uint type);
 
     public static ButtonID ShowMessage(string text, string caption, Flags flags)
     {
         return (ButtonID)MessageBoxW(null, text, caption, (uint)flags);
     }
 }
 #endif

A test case:

 public void Wibble()
 {
     var result = Win32MessageBox.ShowMessage(
         "Would you like me to open the pod bay doors?",
         "HAL 9000",
         Win32MessageBox.Flags.MB_ICONSTOP | Win32MessageBox.Flags.MB_YESNO | Win32MessageBox.Flags.MB_DEFBUTTON2
             | Win32MessageBox.Flags.MB_TASKMODAL | Win32MessageBox.Flags.MB_SETFOREGROUND);
     Debug.LogFormat("Dave says \"{0}, HAL\"", result);
     if (result == Win32MessageBox.ButtonID.No)
         Application.Quit();
 }
 

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by Bunny83 · Jun 04, 2019 at 05:58 PM

There is absolutely no need for an external program. The MessageBox function is part of the win API and is there since i can remember (this goes back to windows 95). The .NET managed class MessageBox in the System.Windows.Forms namespace is essentially just a wrapper around the native API. You can still call the method directly once you added the external declaraion somewhere:

 [DllImport("user32.dll", CharSet=CharSet.Auto)]
 public static extern int MessageBox(IntPtr hWnd, String text, String caption, int options);


Now if you do

 MessageBox(IntPtr.Zero, "This application has crashed!!", "Your application title", 0);

You will get a message box which will block your application until the user closes the message box. Instead of "0" you can also pass for example 0x10 as the last option parameter (which is a bit flag) in order to show the red error icon. For more information on the usage of this method, consult the documentation

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

93 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Unity Build EXE Stuck 0 Answers

Message Box Question(please read) 2 Answers

Creating an .EXE problem. 2 Answers

(Programming languages) Can Javascript/Unityscript be executed as standalone ? 1 Answer

I need a function so that when and event is trigger a message is displayed on the screen for a set duration. 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges