Load Level Data.json

Hello,

I made a game with lots of levels (100) they are in my LevelData.json.

When i’m in level 80 ( for example) and I want to go back in the Menu, I want the game load to level 80 in the game menu not in level 1…

There is my code, my FirstScene ,maybe anyone can help me :

public class LevelScroller : MonoBehaviour
{
    public const string JSON_PATH = "Json/LevelsData";

    public static int maxUnlockedLevel;
    public static int levelSnapped = 1; // Maybe I had to put a PlayerPrefs here ?
    public const int LEVELS_PER_PACK = 20;
    public static bool isLevelDetailActive;

    public ScrollRect levelDetailScrollview;
    public ScrollRect levelPackScrollview;
    public GameObject levelDetailContent;
    public GameObject levelPackContent;
    public GameObject buttonGroupPrefab;
    public GameObject levelPackPrefab;

    [Header("Config")]
    public Color lockedColor;
    // Combien de pourcentage pour les niveaux réussi pour débloquer l'autre pack
    public int packCompletePercentage = 99;

    private string[] data;
    private const string MAX_UNLOCKED_LEVEL_PPK = "SGLIB_MAX_UNLOCKED_LEVEL";

    // Use this for initialization
    void Start()
    {
        //Get Level Data
        data = null;
        string path = JSON_PATH;
        TextAsset textAsset = Resources.Load<TextAsset>(path);
        data = textAsset.ToString().Split(';');
        PlayerPrefs.SetInt("LevelSnap",levelSnapped);

        //Get Level Data resolved
        string[] levelSolvedData = PlayerPrefs.GetString(LevelManager.LEVELSOLVED_KEY).Split('_');
        int highestLevelSolved;

        //get the highest 
        if (levelSolvedData.Length == 1)
        {
            highestLevelSolved = 0;
        }
        else
        {
            highestLevelSolved = int.Parse(levelSolvedData[0].Trim());
            for (int i = 1; i < levelSolvedData.Length; i++)
            {
                if (highestLevelSolved < int.Parse(levelSolvedData[i]))
                {
                    highestLevelSolved = int.Parse(levelSolvedData[i]);
                }
            }
        }

        //Get all levels done in the pack
        float range = highestLevelSolved / LEVELS_PER_PACK;
        List<int> listLevelSolvedInRange = new List<int>();
        if (highestLevelSolved != 0)
        {
            foreach (string o in levelSolvedData)
            {
                if (range > 0)
                {
                    if (int.Parse(o) >= range * LEVELS_PER_PACK && int.Parse(o) <= (range + 1) * LEVELS_PER_PACK)
                    {
                        listLevelSolvedInRange.Add(int.Parse(o));
                    }
                }
                else
                {
                    listLevelSolvedInRange.Add(int.Parse(o));
                }
            }
        }

And in my gameScene.

they’re is my code :

public void GoToHome() // I think this is where I get the PlayerPrefs of levelSnapped ?
    {
        StartCoroutine(LoadScene("FirstScene"));
        SoundManager.Instance.PlayMusic (SoundManager.Instance.gameSound,false);
        SoundManager.Instance.PlayMusic (SoundManager.Instance.background);

Thanks you everyone for your help and time.

Have a nice day.

Best regards,
Shinsuki

Generally you would write something to PlayerPrefs indicating where you are, either just PlayerPrefs.SetInt() with the integer level you’re at, or something more complex like a JSON-encoded string containing that number and any other player game state stuff, like score, lives, ammo, etc.

1 Like