Search Unity

Confirmation when closing app in UWP IL2CPP

Discussion in 'Windows' started by CanisLupus, Oct 28, 2019.

  1. CanisLupus

    CanisLupus

    Joined:
    Jul 29, 2013
    Posts:
    427
    We want the user to be able to save what they are doing before exiting the app using the close button. For other platforms, we register with Application.wantsToQuit, but on UWP this doesn't work.

    In the docs page for Application.quitting it says that in UWP "there's no application quit event".

    There is, but it only works with XAML, not D3D, and it requires declaring an extra capability. Microsoft lets us define a "restricted" capability in the Package.appxmanifest to intercept the app closing event:
    Code (CSharp):
    1.    <Capabilities>
    2.        <rescap:Capability Name="confirmAppClose"/>
    3.    </Capabilities>
    After this, we can use this code on the App.xaml.cs file to register with the event:
    Code (CSharp):
    1.    SystemNavigationManagerPreview.GetForCurrentView().CloseRequested += App_CloseRequested;
    2.  
    3.    private void App_CloseRequested(object sender, SystemNavigationCloseRequestedPreviewEventArgs e)
    4.    {
    5.        Debug.Log("Got here!");
    6.    }
    Instead of printing we could call something in our C# code and do whatever we need.

    However, this is on .NET projects. On IL2CPP things get complicated. It's C++, so in the App.xaml.cpp we do:
    Code (CSharp):
    1.    SystemNavigationManagerPreview::GetForCurrentView()->CloseRequested += ref new EventHandler<SystemNavigationCloseRequestedPreviewEventArgs^>(this, &App::App_CloseRequested);
    2.  
    3.     void App::App_CloseRequested(Platform::Object^ object, SystemNavigationCloseRequestedPreviewEventArgs^ args)
    4.     {
    5.        OutputDebugString("Got here!");
    6.    }
    7.  
    Problem is... what now?
    1. How would this code be able to call our code that was converted from C# to C++ via IL2CPP?
    2. Is there an easier way to do this?
    3. Could this be implemented natively in Unity, so that Application.wantsToQuit works, with the caveat that it needs to use XAML and the extra capability?

    Thanks for any help you can provide.
     
  2. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,678
    You don't have add the code you posted in App.xaml.cpp. You can just do it in your scripts on Unity side:

    Code (csharp):
    1. using UnityEngine;
    2.  
    3. #if ENABLE_WINMD_SUPPORT
    4. using Windows.UI.Core.Preview;
    5. #endif
    6.  
    7. class ConfirmCloseBehaviour : MonoBehaviour
    8. {
    9.     void Awake()
    10.     {
    11. #if ENABLE_WINMD_SUPPORT
    12.         UnityEngine.WSA.Application.InvokeOnUIThread(() =>
    13.         {
    14.             SystemNavigationManagerPreview.GetForCurrentView().CloseRequested += App_CloseRequested;
    15.         }, false);
    16. #endif
    17.     }
    18.  
    19. #if ENABLE_WINMD_SUPPORT
    20.    void App_CloseRequested(object sender, SystemNavigationCloseRequestedPreviewEventArgs e)
    21.    {
    22.        Debug.Log("Got here!");
    23.    }
    24. #endif
    25. }
     
  3. CanisLupus

    CanisLupus

    Joined:
    Jul 29, 2013
    Posts:
    427
    Thanks a lot @Tautvydas-Zilys! You're a lifesaver. :D

    There was an easier way after all. ;)