Hi, if we have more than one, how to access the last scene we were in, in the game, by using Scene Manager, while loading a game. It can be via PlayerPrefs, serialization, all is welcomed…
PlayerPrefs is easy (ok, it can be easy edited too
):
you can make something like autosave script:
void Start () {
PlayerPrefs.SetInt ("lastLevel", SceneManager.GetActiveScene().buildIndex);
}
or set the scene ID manually:
//5 as example
PlayerPrefs.SetInt ("lastLevel", 5);
and then, in script where you will load the scene:
SceneManager.LoadScene (PlayerPrefs.GetInt ("lastLevel"));
you can save the string name too and later load scene with name. PlayerPrefs.SetString
And check PlayerPrefs befor you will use it, I mean:
if (PlayerPrefs.HasKey ("lastLevel")) {
SceneManager.LoadScene (PlayerPrefs.GetInt ("lastLevel"));
} else
//load default start scene
I had some similar idea with the PlayerPrefs, I’ll check out the code you’ve written… My only issue with PlayerPrefs is, it’s hard to access them. I’m using Windows 10, and I’ve also tried serialization, and I like the fact that you can place a file and access it a bit more conveniently in that way. So, I’ll definitely try out the PP method, but do you maybe know how would one go about serializing the scene name/index with binary/xml/json method? That is, if you’ve ever dealt with serialization in your own work. Thanx.
Sorry, I have never used something other as PlayerPrefs.
Check this Unity tutorial:
tutorial link [spoiler]https://unity3d.com/de/learn/tutorials/topics/scripting/persistence-saving-and-loading-data[/spoiler]
Thank you anyway, your code does work… This thread is open for anyone with the knowledge of serialization to chime in… but while we’re at it, cause you said you’ve only used Player Prefs, what is this notion that, especially in some of these serial. tutorials, they say Player Prefs are ‘ultimately’ not good, as in they have certain limitations. Do you find that to be true? Or would you recommend them for the beginners in Unity, so they can further on expand their programming in the future with them as well… Cause if at one point, we have to move to another approach, I was just thinking, maybe it’s better to start with another approach in the beginning as well. Just your thoughts on it…
PlayerPrefs can be easy edited with text editor, so it is not safe. I think, size of PlayerPrefs is limited to one MB. I cannot say it is ultimately not good. It’s easy to use and can be good enough for the project.
Im familiar with serialization. And to clear up with player prefs, its very good at what it does, and its a essentially a lazier version of saving and loading files, and I use it a lot. However, I only use it to store some temporary variables that need to passed from component to component without any direct referencing between these 2 components. And if i need to store larger files or config files for player editing, they i save them in a json file and load them when the game starts.
Btw, theres also SceneManager.sceneLoaded event, which is called when a scene is loaded, heres an example of this.
using UnityEngine.SceneManagement;
public class .... {
void Awake()
{
SceneManager.sceneLoaded += SceneManager_sceneLoaded;
SceneManager.LoadScene("Game");
}
private void SceneManager_sceneLoaded(Scene arg0, LoadSceneMode arg1)
{
Debug.Log(arg0.name);
}
}
Could you maybe write a code for a binary serialization as well? Or, in your case with JSON. Also, for this example with loading scene… Thanx.
Thank you very much, you saved my game. :):)![]()