unity – Tips on how to serialize and deserialize participant stage and place by means of JSON?

unity – Tips on how to serialize and deserialize participant stage and place by means of JSON?

[ad_1]

I completely forgot the way to save a participant’s place and the extent that the participant is in by means of JSON. (I am going to be capable of encrypt and decrypt the information later). Here’s a pattern of my code:

public void SaveGameT()
{

    utilizing FileStream fs = File.Create(persistentPath);
    StreamWriter sr = new StreamWriter(fs);

    PlayerData knowledge = new PlayerData(this);
    knowledge.sceneName = SceneManager.GetActiveScene().title;
    knowledge.place[0] = remodel.place.x;
    knowledge.place[1] = remodel.place.y;
    string json = JsonUtility.ToJson(knowledge, true);
    File.WriteAllText(persistentPath, json);
    Debug.Log("Your knowledge has been saved.");
}

public void LoadGameT()
{
    persistentPath = Utility.persistentDataPath + Path.AltDirectorySeparatorChar + SAVE_FILE;
    if (File.Exists(persistentPath))
    {
        Vector2 newPos = Vector2.zero;
        string json = File.ReadAllText(persistentPath);
        PlayerData knowledge = JsonUtility.FromJson<PlayerData>(json);
        LoadScene(knowledge.sceneName);
        newPos = new Vector2(knowledge.place[0], knowledge.place[1]);
        this.remodel.place = newPos;
    }
    else
    {
        PlayerData knowledge = new PlayerData(this);
        knowledge.sceneName = SceneManager.GetActiveScene().title;
        Vector2 newPos = Vector2.zero;
        this.remodel.place = newPos;
    }
}

I must be reminded about my corrections and precisely the way to repair it.

[ad_2]

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply