Unity實現寫入json文件


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using LitJson;
using System.IO;
using UnityEditor;

public class Person
{
    public string Name { get; set; }
    public double HP { get; set; }
    public int Level { get; set; }
    public double Exp { get; set; }
    public int Attak { get; set; }

}
public class PersonList
{
    public Dictionary<string, string> dictionary = new Dictionary<string, string>();
}

public class Classtext : MonoBehaviour {
    /*定義一個Person對象(其屬性包括,Name,HP,Level,Exp,Attak等),
     將其轉會成json格式字符串並且寫入到person.json的文本中,
     然后將person.json文本中的內容讀取出來賦值給新的Person對象。
     */
	
    public PersonList personList = new PersonList();

    // Use this for initialization
    void Start () {
	    //初始化人物信息
        Person person = new Person();
        person.Name = "Czhenya";
        person.HP = 100;
        person.Level = 30;
        person.Exp = 999.99;
        person.Attak = 38;
		
		//調用保存方法
       Save(person);
        
    }
    /// <summary>
    /// 保存JSON數據到本地的方法
    /// </summary>
    /// <param name="player">要保存的對象</param>
    public void Save(Person player)
    {
        //打包后Resources文件夾不能存儲文件,如需打包后使用自行更換目錄
        string filePath = Application.dataPath + @"/Resources/JsonPerson.json";
        Debug.Log(Application.dataPath + @"/Resources/JsonPerson.json");

        if (!File.Exists(filePath))  //不存在就創建鍵值對
        {
            personList.dictionary.Add("Name", player.Name);
            personList.dictionary.Add("HP", player.HP.ToString());
            personList.dictionary.Add("Level", player.Level.ToString());
            personList.dictionary.Add("Exp", player.Exp.ToString());
            personList.dictionary.Add("Attak", player.Attak.ToString());

        }
        else   //若存在就更新值
        {
            personList.dictionary["Name"] = player.Name;
            personList.dictionary["HP"] = player.HP.ToString();
            personList.dictionary["Level"] = player.Level.ToString();
            personList.dictionary["Exp"] = player.Exp.ToString();
            personList.dictionary["Attak"] = player.Attak.ToString();
        }
       
        //找到當前路徑
        FileInfo file = new FileInfo(filePath);
        //判斷有沒有文件,有則打開文件,,沒有創建后打開文件
        StreamWriter sw = file.CreateText();
        //ToJson接口將你的列表類傳進去,,並自動轉換為string類型
        string json = JsonMapper.ToJson(personList.dictionary);
        //將轉換好的字符串存進文件,
        sw.WriteLine(json);
        //注意釋放資源
        sw.Close();
        sw.Dispose();

        AssetDatabase.Refresh();

    }

    /// <summary>
    /// 讀取保存數據的方法
    /// </summary>
    public void LoadPerson()
    {
        //調試用的  //Debug.Log(1);
        
        //TextAsset該類是用來讀取配置文件的
        TextAsset asset = Resources.Load("JsonPerson") as TextAsset;
        if (!asset)  //讀不到就退出此方法
            return;

        string strdata = asset.text;		
        JsonData jsdata3 = JsonMapper.ToObject(strdata);
		//在這里循環輸出表示讀到了數據,,即此數據可以使用了
        for (int i = 0; i < jsdata3.Count; i++)
        {
            Debug.Log(jsdata3[i]);
        }
        //使用foreach輸出的話會以[鍵,值],,,
		/*foreach (var item in jsdata3)
        {
            Debug.Log(item);
        }*/

    }
	
    private void OnGUI()
    {   //點擊讀取存儲的文件
        if (GUILayout.Button("LoadTXT"))
        {
            LoadPerson();
        }
    }
}


免責聲明!

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



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