Search Unity

[UWP] [ERA] [IL2CPP] How to handle Suspending and Resuming for Windows Store Games?

Discussion in 'Windows' started by SkobbejakGames, Feb 5, 2018.

  1. SkobbejakGames

    SkobbejakGames

    Joined:
    Jun 3, 2016
    Posts:
    396
    Hi,

    I am currently busy with CERT for our first Windows Store title, which is a port of our Xbox One game. Ive gotten everything set up correctly but Im getting a Fail in CERT due to the Connected Storage not being re-initialized when Suspending and Resuming, thus the Connected Storage end up failing after a Suspend/Resume and doesnt save successfully afterwords.

    I'm trying to find the right method of tracking the Suspending and Resuming events in Unity for UWP but I cant seem to find the correct method.

    Theres specific code for Apps which seems to use App.xaml and MainPage.xaml which is generated when building out of Unity so I dont know how I would implement that into my XboxLive script in Unity.

    Then theres the specific method for DirectX games using CoreApplication.Suspending but Im not sure where and how to work it in and what to inlude to make sure everything links up properly.

    Finally there also seems to be the EnteredBackground and LeavingBackground methods that Im also not too sure how they should be implemented or if this is the preferred method, please any help on the matter would be immensely appreciated.
     
  2. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,680
    CoreApplication.Suspending is the right event to use into if you specifically want to track game suspension (as opposed to game pausing). For pause detection you may use OnApplicationPause.

    What do you want to do during suspension?
     
  3. SkobbejakGames

    SkobbejakGames

    Joined:
    Jun 3, 2016
    Posts:
    396
    I just want to be able to Re-Initialize the Same Game System on Resume as my Save game System seems to become unusable when Suspending and Resuming as any data that I try to save after a Suspend/Resume has happened doesnt end up Saving.

    Also how do I call CoreApplication.Suspending or OnApplicationPause? What namespace do I need in order to call it? Are there any classes or anything that I need to set up in order to track it? Can I just connect CoreApplication.Suspending with +=?
     
  4. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,680
    OnApplicationPause is just a MonoBehaviour magic function: https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnApplicationPause.html

    CoreApplication.Suspending is in Windows.ApplicationModel.Core namespace, and you'd need to use #if ENABLE_WINMD_SUPPORT/#endif to access it. And yeah, you can use "+=" operator for it. Remember, you'll get called back on the UI thread in that event callback, and it's unlikely you'll be able to execute any code on Unity's main thread because the app is suspending and Unity has already paused.
     
  5. SkobbejakGames

    SkobbejakGames

    Joined:
    Jun 3, 2016
    Posts:
    396
    Hmm, if executing code would be unlikely during OnSuspend, what do you think would be the best way to handle the situation as I need to be able to re-Initialize the Save System in order to pass Certification.
     
  6. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,680
    Use OnApplicationPause. That happens on Unity's main thread.
     
    SkobbejakGames likes this.
  7. SkobbejakGames

    SkobbejakGames

    Joined:
    Jun 3, 2016
    Posts:
    396
    Awesome. Am I maybe missing something though I cant seem to be able to call OnApplicationPause and doesnt pop up with Intellisense, do I need to include a specific namespace?
     
  8. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,680
    No, you don't call it. Unity calls it. It works like Start() or Update() on MonoBehaviour.
     
  9. SkobbejakGames

    SkobbejakGames

    Joined:
    Jun 3, 2016
    Posts:
    396
    Ah, I see. Sorry for the hassle, it makes sense now. I will test it out and git it a try.

    In case it doesnt work, would an Extended Execution perhaps help (to ensure the code gets called before Suspend gets called)?
     
  10. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,680
    This will work.
     
  11. SkobbejakGames

    SkobbejakGames

    Joined:
    Jun 3, 2016
    Posts:
    396
    Ok, Ive implemented OnApplicationPause and it does get called but my Save System still doesnt want to get re-Initialized. Do you perhaps know what method/code I should be using to reset Connected Storage on Windows Store during Suspend/Resume as I was pretty sure Re-Initializing would work
     
  12. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,680
    Sorry, I've never used connected storage myself.
     
  13. SkobbejakGames

    SkobbejakGames

    Joined:
    Jun 3, 2016
    Posts:
    396
    Ok, I managed to get it to work by just going directly to the InitializeSaveSystem method instead of the elongated manner I tried to do it at first with bool checks and Playmaker. Thanks so much for the advice and all the help. It really helped a ton :)
     
  14. Wecica

    Wecica

    Joined:
    Jul 20, 2016
    Posts:
    27
    Why don't you use CoreApplication.Suspending?
     
  15. Wecica

    Wecica

    Joined:
    Jul 20, 2016
    Posts:
    27
    Hi, SkobbejakGame. Why I have my InitializeSaveSytem fucntion called three times each time I suspend my game?