Search Unity

What to serialize in Unity when app pauses- and why no one talks about that

Discussion in 'Scripting' started by swifter14, Jun 13, 2018.

  1. swifter14

    swifter14

    Joined:
    Mar 2, 2017
    Posts:
    165
    I'm working on a mobile game and the scene contains:
    • 3 different gameObjects- each contains a script to a static class-singleton (3 classes total)
    • 20 gameObject prefabs with properties scripts
    What do I need to serialize, when I get an incoming call for example? You'd think it's the basic thing to do, but none of the tutorials explain that you should serialize when onPause starts.

    By reading the Unity documentation, I can see that there is a `[built-in serialization][1]`, is that true?

    My questions are:

    1. Can I serialize the 3 singleton classes? (one of them is the game manager) I thought that I can't serialize static classes
    2. Does unity use the built-in serialization to store info about the prefab's transform, and other components attached like AudioSource properties, scripts and animation
    3. What happens if the animation is pausing when in the middle of playing?
    I don't want to lose critical info when pausing a game and starting again.


    [1]: https://docs.unity3d.com/Manual/script-Serialization-BuiltInUse.html
     
  2. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,196
    I'm assuming you're not really talking about "pausing" the game, and instead you're talking about resuming the game after it potentially shuts down. (Because you can "pause" the game using `Time.timeScale = 0`, without the need for any serialization.)

    The asset store is full of different "save" packages for saving and restoring state, so you might save yourself some time/effort by getting one of them. Otherwise, there will always be a bit of complexity with this, depending on exactly what data you want to preserve. In some games, it might be enough to keep track of a single value indicating how far a player got in the game, and use that to resume from that checkpoint. Other games completely restore the state of all objects in the scene, right down to the physics characteristics of rigidbodies.

    Anyway, the basic approach is to create a struct/class to store all the data you need, and write methods to store game object data to those objects, serialize the objects, store them somewhere (such as writing to a data file), and then write the code to restore them by instantiating game objects and apply the data to them. It's conceptually simple, but potentially complex to restore every relevant piece of data. For example, you can always persist which animation clip is playing, what state and param values an Animator has, and how far into an animation clip it might be. That's just a lot of little things to capture. If you don't feel like writing that yourself, there's the asset store.
     
    Joe-Censored likes this.
  3. swifter14

    swifter14

    Joined:
    Mar 2, 2017
    Posts:
    165
    Thanks for your answer, yes I'm talking about serializing when
    Code (CSharp):
    1. OnApplicationPause(bool)
    . is called

    I couldn't find it on the asset store. What should I look for? Serialization? By buying such plugin I still need to decide when to call the serialize method and when to deserialize- I guess it should be when
    Code (CSharp):
    1. OnApplicationFocus(bool)
    is called.
    Do I need to save all my 20 gameObject clones on my scene? (How can I be sure their transform properties will be accurate when reopening the app after a pause).
    Isn't it something that we should always do- prevent losing data when receiving a call and save everything? Or am I being too concerned?
     
  4. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,196
    I haven't used it, but this seems pretty popular, and probably does what you want:

    https://assetstore.unity.com/packag...nt/easy-save-the-complete-save-load-asset-768

    If that's expensive, it might still give you an idea what to look for. It also seems to have some ability to tie into the events you're talking about, and possibly automatically save. Not sure.

    As for when you should be saving, that's really your call. In the last mobile game I published, I only saved when the player finished a level. If the game was interrupted for some reason, the player would just start the level over again. That was fine for me. It really depends on the kind of game you're making.
     
  5. ThermalFusion

    ThermalFusion

    Joined:
    May 1, 2011
    Posts:
    906
    I might be totally ignorant about this but I was under the impression the app would be suspended as is and continue automagically when app receives focus again.
    At least this is how two of my unity apps behave on this phone.
    Your phone OS may do something nasty and kill apps that have been suspended too long, if this is the case you may have to serialize and save everything you need and load it when it starts again.
    (OnApplicationQuit)
    Don't take my word for it though as I don't know the specifics.
     
  6. swifter14

    swifter14

    Joined:
    Mar 2, 2017
    Posts:
    165
    Thanks dgoyette, yes the easy way for me would be to restart the level like you did, but it doesn't sound fair for the user to lose his game just because of an incoming call, my main problem is that I still don't understand what do I need to "save", and what is going to be saved as default? Or better to ask- what could get wrong when resuming the game? Do my gameobjects stay in the same transform position? Does it call Start() again when resuming the game?

    ThermalFusion, yes everything works well- until it's not... and then it can lead to exceptions. I don't know what could get removed from the memory when pausing a game and what's not, I still couldn't find a good document about it, it should be a basic thing to think about but it's not a popular issue I guess.
     
  7. swifter14

    swifter14

    Joined:
    Mar 2, 2017
    Posts:
    165
    I still don't understand what do I need to "save". Let me ask again and be more specific

    When we get an incoming call, OnApplicationPause(bool) is being called
    My question is- what am I going to "lose" when resuming the game? I ask that so I know what do I need to "save".

    My game contains
    A) a camera with a GameManager script (singleton class).
    B)Scores UI TEXT (only gets updated when the user wins scores or lose).
    C) 20 object clones (each contains properties scripts)+ an animator component.

    Regarding A- Should I save the entire script? It has a lot of variables like current scores, lists, would I lose them when resuming a game if I don't serialize them?
    Regarding B- Do I need to save the text? Or it simply won't change when resuming a game
    Regarding C- Do I need to save 20 scripts? What about their animation state? Are all the 20 objects going to appear on the screen on the same location like before pausing the game? Or do I need to save their location and create them again?

    I don't understand what is going to be lost when pausing the game for a few minutes and resuming. When I play it on my Android it seems to be fine without saving anything.
     
  8. ErmergerdEnt

    ErmergerdEnt

    Joined:
    Apr 7, 2017
    Posts:
    54
    Why dont you have save points. Use enumerator to save every so often. You can rewrite over the files so you dont have a pile up or just set colliders in game to save at certain points. Or if it's an app then use an enumerator.