i m making a 2d platformer and after the player finishes one level, one bool for each level should be saved and then loaded next session. like if the player completed level1, a bool called level1_done = true should be saved and then loaded next session. the same thing for level2, level3 etc. i already read a bit but i didnt really understood the whole serialization thing. the data should be saved in a XML file
Can the user play levels out of order? If not, Iâd suggest that you can save the âhighest levelâ as an int and that will cover everything you need.
The easiest solution, in my opinion, for small data to be saved is PlayerPrefs (API provided in/with Unity). Check that out.
Not sure if youâre saying you read it should be saved in XML or thatâs what you wantâŚ
If you want some other options/ideas for saving data, please say so. If youâre only interested in XML, I donât know that off the top of my head, sorry
it s like a simple platformer game with a level menu where you can only select the levels you already finished. if you finished a level, the level gets unlocked in the level menu. (so yes, the player can basically play levels out of order)
most people said that playerpref is not a good way to store data and that XML is the easiest one? but yeah, i ll look into playerprefs. thanks!
wow, it s really easy. it works like a charme. thanks a lot, here s my code if anyone else needs it:
i used this simple code in my exit script(if the Player completes the Level):
PlayerPrefs.SetInt("level01_finished", 1);
and in the game menu i check if the player completed the Level. I also give the button a new unlocked sprite:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class levelMenu : MonoBehaviour {
public Sprite unlockedSprite;
public Button level01Btn;
void Start() {
UnlockLevels ();
}
void UnlockLevels() {
int finished01 = PlayerPrefs.GetInt("level01_finished");
if (finished01 == 1) {
level01Btn.image.overrideSprite = unlockedSprite;
// more Code...
}
}
}
Cool
As I was saying, for very simple stuff PlayerPrefs is wonderful.
If you need JSON at some point, that is good. BinaryFormatter is great, too, imo.
It depends on the situation. Iâm glad you got it working
note: if you create a variable like âlevelIntâ on the button, you could fill that in per instance, and use a string or int.ToString() on the GetInt (thereby not needing to hard code levels).* If you hadnât already thought of that, of course.
If you want to learn in detail about saving and loading check out this thread that I made where people posted up a bunch of code and explanations on the subject. It really helped me out and all the code is up to date so you wonât have to screw around too much trying to get everything working.
I know youâre only doing something very simple right now, but if you ever want to do something like save your player position and so on in the future this is what youâll need to learn.