How to program a button in Unity that after being clicked, shows a text with information on the screen?

Asked

Viewed 2,019 times

0

I would not like to have to create another Scene again to show the information... I would like to show on the same screen it and without leaving it swayed. Could someone help me?

  • 1

    Do you want to display a pop-up? Or just a box with the text inside the game?

  • So, I wish it was like this. I have a "class of 2009" button and I want it to show a screen with the information about this class (basically it will be just the names and numbers in the call). About being pop-up or in-game text box, it depends, what’s easier to get dps out of the way would be better (because the user might want to go back to the previous dps screen to see the list) .

1 answer

1

Follow a small C# script to create an object that is a message box:

using UnityEngine;
using System.Collections;

public class MessageBox : MonoBehaviour
{
     //A janela 200x300 px aparecerá no centro da tela.
     private Rect windowRect = new Rect ((Screen.width - 200)/2, (Screen.height - 300)/2, 200, 300);
     //Variavel para controlar a visibilidade.
     private bool show = false;

    void OnGUI () 
    {
        if(show)
            windowRect = GUI.Window (0, windowRect, DialogWindow, "Turma de 2009");
    }

    //Este é o metodo que cria a janela
    void DialogWindow (int windowID)
    {
        float y = 20;

        //Insere um label com o texto desejado
        GUI.Label(new Rect(5,y, windowRect.width, 20), "Texto desejado");

        //Texto para fechar a janela
        if(GUI.Button(new Rect(5,y, windowRect.width - 10, 20), "Fechar"))                 
           show = false;        
    }

    //Para abrir o diálogo de você chama este método no botão que você criou na tela
    public void Open()
    {
        show = true;
    }
}

I couldn’t perform the tests here because I’m without Unity installed

Browser other questions tagged

You are not signed in. Login or sign up in order to post.