首页 > 技术文章 > Unity Json文件的读取与写入

chenxiya 2020-12-29 18:10 原文

主要是用Json文件保存场景内物体的位置名称等信息,并且用的时候需要读取。其实unity本身好像就有相关API,但是我做的还是需要用到LitJson.dll

1、创建需要保存的类

public class EachModel
{
    public string ModelName;
    public string FileSize;
    public string FileName;
    public string PicName;
    public string[] Pos;
    public string[] Roate;
    public string[] Scale;
    public EachModel()
    {
        Pos = new string[3];
        Roate = new string[4];
        Scale = new string[3];
    }
    public EachModel(string ModelName, string FileName, string PicName, string[] Pos, string[] Roate, string[] Scale)
    {
        this.ModelName = ModelName;
        this.FileName = FileName;
        this.PicName = PicName;
        for (int i = 0; i < Pos.Length; i++)
        {
            this.Pos[i] = Pos[i];
            this.Roate[i] = Roate[i];
            this.Scale[i] = Scale[i];
        }
        this.Roate[3] = Roate[3];
    }
}

 以及类的集合

public class ModelList
{
    public List<EachModel> sites = new List<EachModel>();
}

2、将ModelList中sites内容转化成Json文件写入,在StreamingAssets文件夹里创建Config文件夹作为Json文件路径

private void SaveModels(Transform model)
    {

        ModelList r = new ModelList();
        EachModel myModel = new EachModel();
        myModel.Pos = new string[] { model.position.x.ToString("f2"), model.position.y.ToString("f2"), model.position.z.ToString("f2") };
        myModel.Roate = new string[] { model.rotation.x.ToString("f2"), model.rotation.y.ToString("f2"), model.rotation.z.ToString("f2"), model.rotation.w.ToString("f2") };
        myModel.Scale = new string[] { model.localScale.x.ToString("f2"), model.localScale.y.ToString("f2"), model.localScale.z.ToString("f2") };


        //判断config文件是否存在,如果存在就读取Json里的内容来更新site
        if (File.Exists(configPath))
        {
            StreamReader streamreader = new StreamReader(Application.streamingAssetsPath + "/Config/JsonModel.json");//读取数据,转换成数据流
            JsonReader js = new JsonReader(streamreader);//再转换成json数据
            r = JsonMapper.ToObject<ModelList>(js);//读取
            if (IsAdd)
            {
                r.sites.Add(myModel);
            }
            else
            {
                for (int i = 0; i < r.sites.Count; i++)
                {
                    if (r.sites[i].FileName.Equals(fileName.text))
                    {
                        r.sites[i] = myModel;
                        break;
                    }
                }
            }
            streamreader.Close();
            streamreader.Dispose();
        }
        else
        {
            r.sites.Add(myModel);
        }
        //找到当前路径
        FileInfo file = new FileInfo(configPath);
        //判断有没有文件,有则打开文件,,没有创建后打开文件
        StreamWriter sw = file.CreateText();
        string json = JsonMapper.ToJson(r);
        //避免中文乱码
        Regex reg = new Regex(@"(?i)\\[uU]([0-9a-f]{4})");
        json = reg.Replace(json, delegate (Match m) { return ((char)System.Convert.ToInt32(m.Groups[1].Value, 16)).ToString(); });

        //将转换好的字符串存进文件,
        sw.WriteLine(json);
        //注意释放资源
        sw.Close();
        sw.Dispose();
    }

  

 

3、写入Json的时候已经有读取Json的操作了,如果只是读取:

 private ModelList modelDatalList = new ModelList();
    private void UpdateData()
    {
        string path = Application.streamingAssetsPath + "/Config/JsonModel.json";
        if (!File.Exists(path))
        {
            return;
        }
        StreamReader streamreader = new StreamReader(Application.streamingAssetsPath + "/Config/JsonModel.json");//读取数据,转换成数据流
        JsonReader js = new JsonReader(streamreader);//再转换成json数据
        modelDatalList = JsonMapper.ToObject<ModelList>(js);//读取
        streamreader.Close();
        streamreader.Dispose();
    }

  把Json内容转化成ModelList类,输出modelDatalList.sites即可

推荐阅读