- Home /
Saving frequently throughout game lifecycle (Android)
I'm working on a mobile game that saves throughout the game. Namely, I want it to save the game data whenever the app is closed completely and when the app goes out of focus. I'd like to have the game load the save data when the app is opened and when it comes back into focus. I have a rudimentary Save/Load system that I built following one of the live training tutorials, but I'm stuck.
My main question is: where do I call the Save and Load methods in the lifecycle so that it saves and loads when I want it to as described above? I know I can use Start, but what method is the best for when the app comes back into focus or when it leaves focus/closes?
In other words, can I have unity take actions during a specific point in the Android activity lifecycle?
Answer by RuralPlayer · Jun 20, 2018 at 05:55 PM
Add this script to your edit folder, It saves every 5 minuets. You can change the time in the script
using UnityEngine;
using UnityEditor;
[InitializeOnLoad]
public class AutosaveOnRun
{
static AutosaveOnRun()
{
EditorApplication.playmodeStateChanged = () =>
{
if(EditorApplication.isPlayingOrWillChangePlaymode && !EditorApplication.isPlaying)
{
Debug.Log("Auto-Saving scene before entering Play mode: " + EditorApplication.currentScene);
EditorApplication.SaveScene();
EditorApplication.SaveAssets();
}
};
}
}
Thanks, but I don't think this is quite what I was looking for. Does this save the game itself?
you said this will save the game every 5 $$anonymous$$utes, but the code looks like it save the game state changed from external source, is this right or I miss understand smth.
and does this work with android app lifecycle like onPause & onStop?