Пример #1
0
    private static void LoadGame()
    {
        BodyOverviewController controller = FindObjectOfType <BodyOverviewController>();

        if (!controller)
        {
            Debug.LogWarning("Body overview screen must be loaded before save file can be deserialized.");
            return;
        }

        StreamReader stream = new StreamReader(Application.persistentDataPath + "/game.sav");

        try {
            JObject gamedata = JsonConvert.DeserializeObject <JObject>(stream.ReadToEnd());

            m_XP    = gamedata["XP"].ToObject <int>();
            m_level = gamedata["Level"].ToObject <int>();
            //m_inventory = gamedata["Inventory"].ToObject<Dictionary<ICard, int>>();

            JsonConvert.PopulateObject(gamedata["Game"].ToString(), controller);
        }
        catch (Exception) { Debug.LogWarning("Couldn't open save file."); }
        finally {
            stream?.Dispose();
        }
    }
Пример #2
0
    private static void SaveGame()
    {
        BodyOverviewController controller = FindObjectOfType <BodyOverviewController>();

        if (!controller)
        {
            Debug.LogWarning("Body overview screen must be loaded before game can be saved.");
            return;
        }

        var gamedata = new
        {
            XP        = m_XP,
            Level     = m_level,
            Inventory = m_inventory.ToDictionary(x => x.Key.name, x => x.Value),
            Game      = controller
        };

        using (StreamWriter stream = new StreamWriter(Application.persistentDataPath + "/game.sav")) {
            stream.WriteLine(JsonConvert.SerializeObject(gamedata, Formatting.Indented, serializerSettings));
        }
    }