- Home /
How to make a messagebox in Unity
Im im very new to unity, and started off with some tutorials. Also i do a simple test project to get to understand the basic stuff.
Now what i try to do, is make a messagebox pop up when the ESC key is pressed.
if it would have been a win application, i would have done something like this in C#:
if (Input.GetKeyDown(KeyCode.Escape))
{
var message = "exitmessage";
var title = "exitTitle";
var result = MessageBox.Show(
message, // the message to show
title, // the title for the dialog box
MessageBoxButton.YesNo, // show two buttons: Yes and No
MessageBoxImage.Question); // show a question mark icon
// lets see what has been pressed
switch (result)
{
case System.Windows.MessageBoxResult.Yes: // Yes button pressed
Application.Quit();
break;
case System.Windows.MessageBoxResult.No: // No button pressed
break;
default: // Neither Yes nor No pressed (just in case)
MessageBox.Show("Oh noes! What did you press?!?!");
break;
}
}
But it is no Win app, it is Unity. Now, i dont have a clue how i should make something like this in unity. Could someone point me in the right direction with a sample, or a website or something that would help me?
thank you in advance.
Answer by CorruptedHeart · Feb 03, 2012 at 03:27 PM
I started working on a basic Unity port of the .Net MessageBox class. It uses very similar function calls, however it works with callbacks instead of working entirely within the same calling method like the .net version works.
Here's a basic layout of how it would look when calling it.
public MessageBoxButtons buttons = MessageBoxButtons.YesNo;
public MessageBoxIcon icon = MessageBoxIcon.None;
public MessageBoxDefaultButton defaultButton = MessageBoxDefaultButton.Button1;
void Start()
{
MessageBox.Show(Callback, "Hello World!", "Hello", buttons, icon, defaultButton);
}
void Callback(DialogResult result)
{
Debug.Log(result.ToString());
}
I'll do a little code clean up and put it on the Asset Store
It's now available on the Asset Store http://u3d.as/content/corrupted-smile-studio/message-box/2Eo
Answer by HaimBendanan · Dec 31, 2016 at 01:27 PM
I made a message box during the development of my game in Unity, feel free to use it: simple message box for unity - github
Answer by sirjoan620 · Apr 02, 2017 at 12:52 PM
This is an old question, but I'm answering because I see it in Google's search results.
EditorUtility can help in this situation.
if(EditorUtility.DisplayDialog(title,message, "Yes", "No" ))
print("Pressed Yes.");
else
print("Pressed No.");
EditorUtility only works within the editor, it will create an error when you will try to build the application.
Answer by ByteSheep · Feb 03, 2012 at 09:53 AM
You can use unity GUI, for instance:
function OnGUI() {
GUI.Box(Rect(0,0,Screen.width/2,Screen.height/2),"This is the text to be displayed");
}
Here's a good page with the unity GUI basics. You can replace the text with a string variable that can be changed and you can set the gui to only display when you have pressed a certain button etc, but I won't get into that now. If you google it or search on this site I'm sure you'll find plenty of results/similar questions :)
Your answer
![](https://koobas.hobune.stream/wayback/20220613063711im_/https://answers.unity.com/themes/thub/images/avi.jpg)