Unity从固定路径读和写入Json

2019-12-24  本文已影响0人  SeatonLv

在固定路径读写json对于游戏开发中有重要意义,

你可以讲一些需要存档的信息以json的形式保存(需要在发布的可写路径)

也可以在读取某个路径的json信息

比如我们的版本信息就可以这么存储读取

code:

public static void SaveInfoByJson(string jsonData,string fileName)

    {

        string filePath = Application.dataPath + "/Resources/VersionInfos/" + fileName + ".json";

        if (!Directory.Exists(Application.dataPath + "/Resources/VersionInfos/"))

        {

            Directory.CreateDirectory(Application.dataPath + "/Resources/VersionInfos");

        }

        StreamWriter sw = new StreamWriter(filePath);

        sw.Write(jsonData);

        sw.Close();

    }

    public static VersionJsonInfo GetCurVersionInfo(string fileName)

    {

        VersionJsonInfo versionInfo = null;

        string filePath = Application.dataPath + "/Resources/VersionInfos/" + fileName;

        if (File.Exists(filePath))

        {

            StreamReader sr = new StreamReader(filePath);

            string jsonStr = sr.ReadToEnd();

            sr.Close();

            versionInfo = JsonMapper.ToObject<VersionJsonInfo>(jsonStr);

            //Debug.Log(versionInfo.downloadUrl + "  " + versionInfo.fileSize);

        }

        return versionInfo;

    }

上一篇 下一篇

猜你喜欢

热点阅读