- Home /
When Gui Text Clicked, Quit Game
Hey guys, i need help on making a gui text a quit button. i already made a font and a text, so i just need help making a quit button. thanks!
Answer by Lukamyte · Sep 29, 2013 at 11:20 PM
the code for the button should look something like
function OnGUI(){
if(GUI.Button(Rect(0, 0, 200, 50), text)){
Application.Quit();
}
}
Or in C#,
private void OnGUI()
{
if(GUI.Button(new Rect(0, 0, 200, 50), "" + text, "Your_Custom_Style"))
{
Application.Quit();
}
}
Either way, @Lukamyte is correct.
Answer by LukaKotar · Sep 29, 2013 at 11:49 PM
You will need to use GUIStyle to use your custom font. To quit the game, you can use Application.Quit()
. To create a button, look at the reference to GUI.Button.
Here are some code examples:
C#:
using UnityEngine;
using System.Collections;
public class QuitButton : MonoBehavior{
public Font font;
private GUIStyle guiStyle;
void Start(){
guiStyle.font = font;
}
void OnGUI(){
if(GUI.Button(new Rect(10, 10, 100, 60), "Quit Game", guiStyle)){
Application.Quit();
}
}
}
Unity-JavaScript:
var font : Font;
private var guiStyle : GUIStyle;
function Start(){
guiStyle.font = font;
}
function OnGUI(){
if(GUI.Button(Rect(10, 10, 100, 60), "Quit Game", guiStyle)){
Application.Quit();
}
}
I'd avoid allocations for each OnGUI()
message, probably better to allocate the GUIStyle
during Start()
ins$$anonymous$$d.
Your answer
Follow this Question
Related Questions
Level Button And Quit Button Android 2 Answers
reference Gui texture outside script 1 Answer
[Closed] MainMenu Script error 2 Answers
Destroy a game object after a few seconds of being triggered? 2 Answers
How To Make GUI Buttons Load/Quit 1 Answer