@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();
}