- Home /
How do I create an Exit confirmation Pop up when I click the exit button?
The question is pretty straight forward…But also I want to make a pop up asking whether you want to exit of not with a yes or no button(I have got all the sprite assets for it) . When I hit yes it must close the application which I did it but I don’t get how to make the popup quote image visible and all the background menu items (such as play button etc) invisible and also I don’t know how to again make this exit pop up invisible when I hit No button and bring back my background UI items?
Answer by Paps O · Dec 27, 2014 at 06:22 PM
I made a small example for you
attach the following script on a gameobject (the canvas for example)
add to the root of your normal ui (a panel for example) a CanvasGroup component
add to the root of your quit confirmation ui (a panel for example) a CanvasGroup component
Link the two canvas group to the gameobject where you put the script
For each button (Quit, Confirm yes, Confirm No) add in the Inspector a onclick event to the script corresponding method
That is all i think (I tried to comment a bit the code for reference), I also made some screenshot just in case
public class QuitHandler : MonoBehaviour { public CanvasGroup uiCanvasGroup; public CanvasGroup confirmQuitCanvasGroup;
// Use this for initialization private void Awake() { //disable the quit confirmation panel DoConfirmQuitNo(); } /// <summary> /// Called if clicked on No (confirmation) /// </summary> public void DoConfirmQuitNo() { Debug.Log("Back to the game"); //enable the normal ui uiCanvasGroup.alpha = 1; uiCanvasGroup.interactable = true; uiCanvasGroup.blocksRaycasts = true; //disable the confirmation quit ui confirmQuitCanvasGroup.alpha = 0; confirmQuitCanvasGroup.interactable = false; confirmQuitCanvasGroup.blocksRaycasts = false; } /// <summary> /// Called if clicked on Yes (confirmation) /// </summary> public void DoConfirmQuitYes() { Debug.Log("Ok bye bye"); Application.Quit(); } /// <summary> /// Called if clicked on Quit /// </summary> public void DoQuit() { Debug.Log("Check form quit confirmation"); //reduce the visibility of normal UI, and disable all interraction uiCanvasGroup.alpha = 0.5f; uiCanvasGroup.interactable = false; uiCanvasGroup.blocksRaycasts = false; //enable interraction with confirmation gui and make visible confirmQuitCanvasGroup.alpha = 1; confirmQuitCanvasGroup.interactable = true; confirmQuitCanvasGroup.blocksRaycasts = true; } /// <summary> /// Called if clicked on new game (example) /// </summary> public void DoNewGame() { Debug.Log("Launch a new game"); }
I hope it's clear enough
Wow Thanks man, for this awesome tutorial... Though I have achieved it somehow which kinda works but I will change it to this method because that one is definitely not a good method.... But actually now I have got into a bigger problem.. Can you please check my other question on the button issue I have posted and please let me know whether it is my software problem or my code???
Thank you very much for the code and the guide, helped a lot! :)
Answer by HugoZandel · May 08, 2015 at 08:45 PM
I would like to add an alternative answer but it will only work in a standalone player on desktops. The advantage here is that if you run in window mode the Windows close button will trigger OnApplicationQuit thus giving more feedback to the user.
This is not a copy/paste type of example but more of an alternate method to the above. You will still need to code stuff.
public class ExampleClass : MonoBehaviour {
private bool allowQuitting = false;
void Awake() {
DontDestroyOnLoad(this.gameObject);
}
void OnApplicationQuit() {
//TODO : SHOW POPUP CONTAINING A YES BUTTON WITH
//Application.Quit() + change allowQuitting to true.
if (!allowQuitting)
Application.CancelQuit();
}
}
Answer by unity_4zuHSdl8nHRlfQ · Nov 09, 2018 at 05:19 PM
awesome work but im have diferent code and gift effect fade
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class exitConfirm : MonoBehaviour {
public CanvasGroup uiCanvasGroup;
public CanvasGroup confirmQuitCanvasGroup;
private void Start () {
}
public void FadeIn()
{
StartCoroutine(FadeCanvasGroup(confirmQuitCanvasGroup, confirmQuitCanvasGroup.alpha, 1, .5f));
confirmQuitCanvasGroup.blocksRaycasts = true;
}
public void FadeOut()
{
StartCoroutine(FadeCanvasGroup(confirmQuitCanvasGroup, confirmQuitCanvasGroup.alpha, 0, .5f));
confirmQuitCanvasGroup.blocksRaycasts = false;
}
public IEnumerator FadeCanvasGroup(CanvasGroup cg, float start, float end, float lerpTime = 1)
{
float _timeStartedLerping = Time.time;
float timeSinceStarted = Time.time - _timeStartedLerping;
float percentageComplete = timeSinceStarted / lerpTime;
while (true)
{
timeSinceStarted = Time.time - _timeStartedLerping;
percentageComplete = timeSinceStarted / lerpTime;
float currentValue = Mathf.Lerp(start, end, percentageComplete);
cg.alpha = currentValue;
if (percentageComplete >= 1) break;
yield return new WaitForFixedUpdate();
}
print("done");
}
Your answer
Follow this Question
Related Questions
Override sorting not working? The canvas just freezes………zzzzz 0 Answers
How to create a gradient progress bar 3 Answers
[4.6] Visual Studio Tools and UnityEngine.UI reference 3 Answers
Unity UI 4.6 - Programmatically adding events - EventTrigger.delegates is null 0 Answers
Unity 4.6 UI - Ignore raycast -1 Answers