Skip to content

Instantly share code, notes, and snippets.

@greggman
Created May 11, 2016 09:47
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save greggman/98c2b2d2e936ea71f544ec6ceb760374 to your computer and use it in GitHub Desktop.
Save greggman/98c2b2d2e936ea71f544ec6ceb760374 to your computer and use it in GitHub Desktop.
Simple modal dialog for Unity / Unity3D
using UnityEngine;
using System;
// example:
// HFTDialog.MessageBox("error", "Sorry but you're S.O.L", () => { Application.Quit() });
public class HFTDialog : MonoBehaviour {
Rect m_windowRect;
Action m_action;
string m_title;
string m_msg;
static public void MessageBox(string title, string msg, Action action)
{
GameObject go = new GameObject("HFTDialog");
HFTDialog dlg = go.AddComponent<HFTDialog>();
dlg.Init(title, msg, action);
}
void Init(string title, string msg, Action action)
{
m_title = title;
m_msg = msg;
m_action = action;
}
void OnGUI()
{
const int maxWidth = 640;
const int maxHeight = 480;
int width = Mathf.Min(maxWidth, Screen.width - 20);
int height = Mathf.Min(maxHeight, Screen.height - 20);
m_windowRect = new Rect(
(Screen.width - width) / 2,
(Screen.height - height) / 2,
width,
height);
m_windowRect = GUI.Window(0, m_windowRect, WindowFunc, m_title);
}
void WindowFunc(int windowID)
{
const int border = 10;
const int width = 50;
const int height = 25;
const int spacing = 10;
Rect l = new Rect(
border,
border + spacing,
m_windowRect.width - border * 2,
m_windowRect.height - border * 2 - height - spacing);
GUI.Label(l, m_msg);
Rect b = new Rect(
m_windowRect.width - width - border,
m_windowRect.height - height - border,
width,
height);
if (GUI.Button(b, "ok"))
{
Destroy(this.gameObject);
m_action();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment