Unity使用Newtonsoft Json实现持久数据存储

一、导入包

Newtonsoft Json

NewtonsoftJson是c#的一套json处理封装类,它可以高效,方便地帮助我们处理json。

在这里插入图片描述

二、存储信息,读取信息脚本

1.基础方法

JsonConvert.DeserializeObject<T>(string json)//反序列化
JsonConvert.SerializeObject(object obj) //序列化

2.完整代码示例

using Newtonsoft.Json;
using System.IO;
using UnityEngine;
using Syste.Collections.Generic;

public class StoreInformation : MonoBehaviour
{
    public List <int> scoreList;//分数的列表
    private int score;//得分
    private string dataPath;//存储的路径
    private void Awake()
    {
        //Application.persistentDataPath是序列化存储的路径,不同设备路径不同
        dataPath = Application.persistentDataPath + "/score.json";
        scoreList = GetScoreListData();//读取保存的文件
        //场景转换时不要销毁该物体
        DontDestroyOnLoad(this);
    }
    public void GetScore(int i)//得到分数
    {
        score = i;
    }
    public void GameOver()//游戏结束时,将不同的分数保存
    {
        if(!scoreList.Contains(score))
        {
            scoreList.Add(score);
        }
        scoreList.Sort();//排序:由小到大
        scoreList.Reverse();//倒置
        //写文件(文件路径,文件内容(序列化的列表))
        File.WriteAllText(dataPath,JsonConvert.SerializeObject(scoreList));
    }
    private List<int> GetScoreListData()
    {
        if(File.Exists(dataPath))
        {
            //读文件
            string jsonData = File.ReadAllText(dataPath);
            //读出来的数据反序列化
            return JsonConvert.DeserializeObject<List<int>>(jsonData);
        }
        return new List<int>();
    }
}

三、保存路径

Application.persistentDataPath在不同设备有不同的保存位置

Windows: %userprofile%\AppData\Local\Packages\<productname>\LocalState

Linux: $XDG_CONFIG_HOME/unity3d/<companyname>/<productname>

Mac: ~/Library/Application Support/companyname/productname

旧版本Mac:~/Library/Application Support/unity.companyname.productname.

Andriod: /Data/Data/com.<companyname>.<productname>/files

Andriodsd卡: /storage/sdcard0/Android/data/com.<companyname>.<productname>/files

IOS: /var/mobile/Containers/Data/Application/<RandomFolderName>/Documents

companyname =构建设置中的公司名称

productname =构建设置中的产品名称

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

睡不着乌托托

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值