美文网首页
Unity从固定路径读和写入Json

Unity从固定路径读和写入Json

作者: SeatonLv | 来源:发表于2019-12-24 16:11 被阅读0次

    在固定路径读写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;

        }

    相关文章

      网友评论

          本文标题:Unity从固定路径读和写入Json

          本文链接:https://www.haomeiwen.com/subject/corfoctx.html