How to create a dialog box in Unity (without using UnityEditor)?

I want to use dialog boxes (having two options).

I tried UnityEditor, but when I create a project to create an exe file, it does not work, because scripts containing UnityEditor links just work in edit mode. After searching the Internet for hours, there were two sentences (both did not work).

First one . Using #if UNITY_EDITOR before the code and ending with #endif . In this case, it was built without errors, but there were no dialog boxes in my game.

Second one . Place the script in the Assets / Editor directory. In this case, I could not add a script to my game object. Perhaps by creating a new script in the "Editor" section and inserting the lines used by UnityEditor, it would work, but I could not figure out how to do this.

I used:

 #if UNITY_EDITOR if (UnityEditor.EditorUtility.DisplayDialog("Game Over", "Again?", "Restart", "Exit")) { Application.LoadLevel (0); } else { Application.Quit(); } #endif 

I also tried adding “using UnityEditor” and encapsulating it with the preprocessor command that I mentioned. It is also useless.

Does anyone know how to use UnityEditor in startup mode or how to create dialogs differently?

+7
c # dialog unity3d
source share
2 answers

If I understand correctly, you need a pop-up window when the character dies (or the player does not work). UnityEditor classes are designed to extend the editor, but in your case you need a solution for the game. This can be achieved using gui windows.

Below is a short script in C # that gives this.

 using UnityEngine; using System.Collections; public class GameMenu : MonoBehaviour { // 200x300 px window will apear in the center of the screen. private Rect windowRect = new Rect ((Screen.width - 200)/2, (Screen.height - 300)/2, 200, 300); // Only show it if needed. private bool show = false; void OnGUI () { if(show) windowRect = GUI.Window (0, windowRect, DialogWindow, "Game Over"); } // This is the actual window. void DialogWindow (int windowID) { float y = 20; GUI.Label(new Rect(5,y, windowRect.width, 20), "Again?"); if(GUI.Button(new Rect(5,y, windowRect.width - 10, 20), "Restart")) { Application.LoadLevel (0); show = false; } if(GUI.Button(new Rect(5,y, windowRect.width - 10, 20), "Exit")) { Application.Quit(); show = false; } } // To open the dialogue from outside of the script. public void Open() { show = true; } } 

You can add this class to any of your game objects and call it Open mehtod to open a dialog.

+5
source share

See the Unity GUI Scripting Guide .

Example:

 using UnityEngine; using System.Collections; public class GUITest : MonoBehaviour { private Rect windowRect = new Rect (20, 20, 120, 50); void OnGUI () { windowRect = GUI.Window (0, windowRect, WindowFunction, "My Window"); } void WindowFunction (int windowID) { // Draw any Controls inside the window here } } 

Alternatively, you can show the textured plane in the center of the camera.

+1
source share

All Articles