Unity Json文件的讀取與寫入


主要是用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即可


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM