Unity

How to add exit dialog popup in your game using Unity 5.3 ?

How to add exit dialog popup in your game using Unity 5.3 ?
How to add exit dialog popup in your game using Unity 5.3 ?

In this tutorial, I’m going to describe how to add an exit confirmation dialog box for your unity game without creating and maintaining a graphics UI.

Basically, when user hits back button device generates a pop-up dialog which asks user “Do you want to quit the game ” with  “Yes” and “No” button

Before starting coding we need a package called ‘Mobile Native PopUps‘. https://www.assetstore.unity3d.com/en/#!/content/18479

asset

download it from unity asset store (it’s free) and import it to your project.

Now create a new c# script a called AndroidExit and add this code  in it.

Now compile and build your project

Code snippet

using UnityEngine;
using System.Collections;
public class AndroidExit : MonoBehaviour
{
    
#if UNITY_ANDROID

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Escape))
        {
            MobileNativeDialog dialog = new MobileNativeDialog("Quit Game", "Do you want to quit the game ?");
            dialog.OnComplete += OnDialogClose;
        }
#endif
    }
    private void OnDialogClose(MNDialogResult result)
    {
        //parsing result
        switch (result)
        {
            case MNDialogResult.YES:
                //Debug.Log("Yes button pressed");
                Application.Quit();
                break;
            case MNDialogResult.NO:
                Debug.Log("No button pressed");
                break;

        }
    }
   
}

That’s it. Let me know your experiences in the comment section