Unity 读取配置文件json

:2018-08-07    :219 次   :2493 字   

1.配置文件config.json


{
   "name":"Base",
   "tag":null,
   "step":1,
   "son__up_number":4,
   "son_up_name":["_bar1","_bar2","_bar3","_bar4"],
   "son_dowm_number":0,
   "son_up_name":null
}


2.解析类Config.cs


[System.Serializable]
public class Config
{
    public string name;
    public string tag;
    public int step;
    public string[] son_up_name;
}



3.调用脚本


using UnityEngine;
using System.IO;
using System.Text;
using UnityEngine.UI;
public class JsonTest : MonoBehaviour
{
    // Use this for initialization
    void Start()
    {
        string jsonTest = File.ReadAllText("C:/Users/GameGeng/Desktop/config.json", Encoding.UTF8);
        Config obj = JsonUtility.FromJson<Config>(jsonTest);
        Debug.Log(obj.name);
        Debug.Log(obj.step);
        Debug.Log(obj.tag);
        GetComponent<Text>().text = obj.name;
        foreach (var inter in obj.son_up_name)
        {
            Debug.Log(inter);
        }
    }
    //保存Json
    void WriteJson(Config gameStatus)
    {
        string json = JsonUtility.ToJson(gameStatus);
        string savePath = Application.dataPath + "/Json/" + gameStatus.name + ".json";
        File.WriteAllText(savePath, json, Encoding.UTF8);
    }
}






    暂无评论,快来抢沙发吧!
×