Add Confirmation UI on EXIT (Android Support)

Share custom commands, script functions, actor implementations and other Naninovel plug-ins you've created.
Post Reply
Restush
Posts: 8
Joined: 31 Aug 2020 08:26

Add Confirmation UI on EXIT (Android Support)

Post by Restush »

Hello, I think I will add my script here so I can find it easily and not being removed again by a virus or accidentally formated by my HDD.

This script will add confirmation UI on exit. Support the back button on Android which makes a player can exit while playing a script.

How? Attached the TitleExitButton below to ExitButton on TitleUI prefab and disable/remove the original TitleExitButton (Script).
Where to save? I think, save to any folder should works.

TitleExitButton.cs

Code: Select all

using Naninovel;
using Naninovel.UI;
using UnityEngine;
public class TitleExitButton : ScriptableButton
{
    [ManagedText("MyManagedText")]
    public static string TitleExitConfirmation = "";
    private IUIManager uIManager;
    private IConfirmationUI confirmationUI;
    protected override void Awake()
    {
        base.Awake();
        uIManager = Engine.GetService<IUIManager>();
     }
    protected override void Start()
    {
        base.Start();
        confirmationUI = uIManager.GetUI<IConfirmationUI>();
    }
    protected override void OnButtonClick()
    {
        uIManager.GetUI<IPauseUI>()?.Hide();
        ExitGameAsync();
    }
    private async void ExitGameAsync()
    {
        if (!await confirmationUI.ConfirmAsync(TitleExitConfirmation)) return;
        if (Application.platform == RuntimePlatform.WebGLPlayer)
            Application.OpenURL("about:blank");
        else Application.Quit();
    }
    protected override void Update()
    {
        if (Application.platform == RuntimePlatform.Android)
        {
            // Check if Back was pressed this frame
            if (Input.GetKeyDown(KeyCode.Escape))
            {
                // Quit the application
                BackButton();
            }                       
        }
    }
    private async void BackButton()
    {
        if (!await confirmationUI.ConfirmAsync(TitleExitConfirmation)) return;
        Application.Quit();
    }
}

Custom Confirmation Text

I use custom managed text for text confirmation. You can see there is empty string on TitleExitConfirmation.

Code: Select all

[ManagedText("MyManagedText")]
public static string TitleExitConfirmation = "";

Just make a new managed text on your managed text folder > Right Click > Create > Naninovel > Managed Text then write this inside.
TitleExitButton.TitleExitConfirmation: Exit Game?
Naninovel will generate the string itself of 'Exit Game?'.

Post Reply