Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create dialogbox in Unity (not using UnityEditor)?

Tags:

c#

dialog

unity3d

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

I tried UnityEditor, but when I build the project to create an exe file, it didn't work because scripts having UnityEditor references are just working in edit mode. After searching on the Internet for hours, there were two suggestions (both didn't work).

First one: Using #if UNITY_EDITOR before code and ending with #endif. In this case, It was built without an error but there were no dialog boxes in my game at all.

Second one: Putting the script under Assets/Editor directory. In this case, I couldn't add the script to my game object. Maybe, creating a new script under Editor directory and pasting UnityEditor used lines in it would work but I couldn't figured 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 I mentioned. It is also useless.

Is there anyone knowing how to use UnityEditor in run mode or how to create dialog boxes in a different way?

like image 628
Mustafa Orkun Acar Avatar asked Aug 25 '13 23:08

Mustafa Orkun Acar


People also ask

How do I show messages in unity?

Open Unity and start a new project. Then from the Hierarchy, click 'Create' and navigate to UI > Text and add the Text UI to the Hierarchy. It will appear as a child of the Canvas in the Hierarchy.

Are Unity scripts C#?

Scripts are written in a special language that Unity can understand. And, it's through this language that we can talk to the engine and give it our instructions. The language that's used in Unity is called C# (pronounced C-sharp). All the languages that Unity operates with are object-oriented scripting languages.


1 Answers

if I understand right, you need a popup window, when the character dies (or the player failed). The UnityEditor classes are for extending the editor, but in your case you need an in game solution. This can be achived with gui windows.

Here is a short script in c# that achives 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 its Open mehtod to open the dialogue.

like image 64
Barefoot Games - Avatar answered Sep 30 '22 13:09

Barefoot Games -