Hey so I setup the wantsToQuit and quitting listeners, what I want to create is a “Are you sure you wish to close” popup before closing the application below is my implementation but 9 times out of 10 my game would crash or the popup will appear and then it’d crash.
So far its only popped up and functioned proprly before.
It’d be much appreciated if somebody could give me some advice for approuching this issue.
I think you are calling Quit a lot of times and causing a Stack Overflow.
Read your output.log file in the AppData folder for your game, or put a break point in the editor, debug it out.
I use wantsToQuit and it works flawlessly with Alt + F4 and X button events.
It makes no sense for this event to be OS dependent, it is kind of obvious that Unity fires up this event on the appropriate way for each OS, depending on your build settings.
The documentation has only one situation where it behaves differently that expected:
IMPORTANT: The return has no effect on iPhone. Application can not prevent termination under iPhone OS.
But then again, Apple is Apple, what to expect :-\
This is how I currently do it:
I hook into Application.wantsToQuit and link it to my CallQuitWindow-Function, which will open my confirmation window and return false, to abort the quitting.
If the confirmation button is pressed, I unsubscribe my Function from the event, and than call Application.Quit() normally.
I hope this guides you into the right direction.
using UnityEngine;
public class ConfirmQuitting : MonoBehaviour
{
[SerializeField] private GameObject confirmationWindow;
private static ConfirmQuitting instance;
[RuntimeInitializeOnLoadMethod]
static void RunOnStart()
{
Application.wantsToQuit += CallQuitWindow;
}
private void Awake()
{
instance = this;
}
public static bool CallQuitWindow()
{
// Call Confirmation-Window and abort the quitting
instance.confirmationWindow.SetActive(true);
return false;
}
// Attached to Conformation-Button
public void Confirm()
{
// When pressing Confirmation-Button, unsubscribe from Event and the Quit Application
Application.wantsToQuit -= CallQuitWindow;
Application.Quit();
}
}