Monday, 6 February 2012

.NET Message Boxes in Unity

While browsing the answer.unity3d.com site I came across a question on how to make a message box in Unity, so I set about recreating as much of the message box functionality as possible in Unity's standard GUI system. Having just implemented a very similar system in order to display Debug messages to the screen it proved rather simple.

Without further ado I present to you MessageBox, a Unity MessageBox implementation of the .NET MessageBox. Complete with selectable buttons and icons. It uses Callbacks instead of the inline way that the .NET version does, however it is similar in every other way.


Runtime
Supports almost every overloaded method of MessageBox.Show(), it does not support any of the IWin32Window or Help methods.

.NET Example:
var result = MessageBox.Show("This is the text", "This is the Title", MessageBoxButtons.YesNo);
if(result == DialogResult.Yes)
{
   Console.Write("Yes");
}
else
{
   Console.Write("No");
}

Unity Example:
void Start()
{
   MessageBox.Show(HandleMessage,"This is the text", "This is the Title", MessageBoxButtons.YesNo);
}

void HandleMessage(DialogResult result)
{
   if(result == DialogResult.Yes)
   {
      Console.Write("Yes");
   }
   else
   {
      Console.Write("No");
   }
}
Which is very similar with the only change being the callback in the beginning (HandleMessage) which can be any method as long as it accepts a DialogResult parameter.

Version: 1.0
Unity Version: 3.4.1 Free
Price: $2
Asset Store Link: http://u3d.as/content/corrupted-smile-studio/message-box/2Eo
Web Player Demo.

Regards,
Garth

No comments:

Post a Comment