Unity LitJson-based Archiving System

Unity LitJson-based archiving system:

Description:

Recently I was playing my first game because I used to play it casually, so it wasn't very deep. Yesterday I saw LitJson's data persistence, and today I'm trying to do it. From the morning to now, although the code may be insufficient, at least the function is implemented, and further modifications are not a big deal. I also summarized some pits.

Simple use of LitJson

  1. Download a LitJson.dll and place it under Assets/Plugins
  2. using LitJson in scripts
  3. Create an SL class to hold the data you need to store. To serialize [System.Serializable]
  4. SaveObjectToJson Archive
  private void SaveObjectToJson()
    {
        save.scene = GameManager.getSceneIndex();
        save.posX = GameManager.instance.player.transform.position.x;
        save.posY = GameManager.instance.player.transform.position.y;
        Debug.Log(save.posX); Debug.Log(save.posY);
        File.WriteAllText(Application.dataPath + "/Json/sl.json", JsonMapper.ToJson(save));
    }
  1. LoadJsonFromFile Reader
   private string LoadJsonFromFile()
    {
        return File.ReadAllText(Application.dataPath + "/Json/sl.json");
    }

SL:

Main interface

- Start (controlled by MainMenu script in Canvas, buildIndex+1 after clicking) - Load (controlled by GameManager object (created in the Hierarchy window with GameManager and DataManager scripts inside), click and call DataManager, then click to get the saved json file and convert it into a custom class (SL in this article) , assign the data in SL to the corresponding data in GameManager, and finally LoadScene)
    public void LoadGame()
    {
        load = JsonMapper.ToObject<SL>(LoadJsonFromFile());
        GameManager.instance.scene = load.scene;
        GameManager.instance.pos = new Vector3((float)load.posX, (float)load.posY, 0f);
        GameManager.changeScene(load);
    }

Pause interface

  • Save (controlled by the DataManager in GameManager, clicks to assign the corresponding data in GameManager to the newly defined SL class variable save, and finally saves with the File.WriteAllTest function).
    public void SaveGame()
    {
        SaveObjectToJson();
    }
    private void SaveObjectToJson()
    {
        save.scene = GameManager.getSceneIndex();
        save.posX = GameManager.instance.player.transform.position.x;
        save.posY = GameManager.instance.player.transform.position.y;
        Debug.Log(save.posX); Debug.Log(save.posY);
        File.WriteAllText(Application.dataPath + "/Json/sl.json", JsonMapper.ToJson(save));
    }

Be careful
These codes don't write well, they're too complicated, they can be optimized, and the big guys just laugh.

Stepped in a pit for a long time

1. The code is too cluttered, you can see a lot of scripts from above, I have spent some time actually doing it myself, and I will see if I can simplify it in the future.
2.On Click under Button drags in prefabricated scripted objects best, since it doesn't need to be instantiated every time the interface is loaded, for example, when my button on the main menu is an object with GameManager in Hierarchy, and when the main menu is loaded into Scene 1 and returned to the menu, the On Click on the main menu is Missing. (When I found out that this problem was still caused by irregularity in 233, I placed a prefabricated object in On Click in Scene 1 and it suddenly came to mind that the above problem was solved as well)
3. Role GameObject issues. The first time I wrote the script, there was a problem. I placed the role instantiation after LoadScene, Debug came out and GameObject was null in any case, and finally rewritten it without changing it for a long time. After rewriting, I assign my GameObject to GameManager in Start in PlayerController, so that once loaded into Scenario 1, I can instantiate the GameObject in GameManager and pass the real-time position ing of the role to pos in GameObject in Update of GameObject (guys, what I think, I'll change it when I'm done writing), and assign it when I save it.

Posted by kenshintomoe225 on Mon, 22 Nov 2021 13:56:28 -0800