使用Json本地存储数据

2019-07-13  本文已影响0人  浪荡少年

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

using LitJson;

using System.IO;

using UnityEditor;

using System;

public class WorkList

{

    public Dictionary<string, string> dictionary = new Dictionary<string, string>();

}

public class Work//任务

{

    public string Name;//任务对象的名称

    public string fishId;//任务对象的编号

    public string num;//任务需要的数量

    public string jindu;//任务进度(需要不断的修改)

    public string renwu;//任务几 图片精灵的路径(Res/ZZZ/everyday/renwu+编号)

    public string jiang1;//奖1

    public string jiang2;//奖2

    public string week;//星期几

}

public class Classtext : SingletonMono<Classtext>

{

    /*定义一个work01对象(其属性包括,Name,HP,Level,Exp,Attak等),

    将其转会成json格式字符串并且写入到work01.json的文本中,

    然后将work01.json文本中的内容读取出来赋值给新的work01对象。

    */

    public WorkList WorkList = new WorkList();

    JsonData jsdata3;

    StreamWriter sw;

    public Work work;

    string[] weekdays = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };

    // Use this for initialization

    void Awake()

    {

        if (PDataPath())

        {

            work = Loadwork01();

            if (work.week == DateTime.Now.DayOfWeek.ToString())

            {

                return;

            }

            else

            {

                Init();

                work = Loadwork01();

            }

            return;

        }

        //初始化任务信息

        Init();

        work = Loadwork01();

    }

    public void Init()

    {

        Work work01 = new Work();

        work01.Name = "绿鳄鱼|紫水母|蓝鲨鱼|老鼠鱼|灯笼鱼";

        work01.fishId = "1|2|3|4|5";//

        work01.num = "100|80|60|40|20";

        work01.jindu = "0|0|0|0|0";

        work01.renwu = "1|2|3|4|5";

        work01.jiang1 = "25|20|15|10|5";

        work01.jiang2 = "5|10|15|20|25";

        work01.week = DateTime.Now.DayOfWeek.ToString();

        Save(work01);

    }

    public bool PDataPath()//判断该路径是否存在

    {

        string filePath = Application.dataPath + @"/Resources/work" + GameData.Instance.userName + ".json";

        if (File.Exists(filePath))

        {

            Debug.Log("文件已存在" + GameData.Instance.userName);

            return true;

        }

        else

        {

            return false;

        }

    }

    /// <summary>

    /// 保存JSON数据到本地的方法

    /// </summary>

    /// <param name="player">要保存的对象</param>

    public void Save(Work player)

    {

        //打包后Resources文件夹不能存储文件,如需打包后使用自行更换目录

        string filePath = Application.dataPath + @"/Resources/work" + GameData.Instance.userName + ".json";

        if (!File.Exists(filePath))  //不存在就创建键值对

        {

            WorkList.dictionary.Add("Name", player.Name);

            WorkList.dictionary.Add("FishId", player.fishId);

            WorkList.dictionary.Add("Num", player.num);

            WorkList.dictionary.Add("JinDu", player.jindu);

            WorkList.dictionary.Add("RenWu", player.renwu);

            WorkList.dictionary.Add("Jiang1", player.jiang1);

            WorkList.dictionary.Add("Jiang2", player.jiang2);

            WorkList.dictionary.Add("Week", player.week);

            //WorkList.dictionary.Add("Text", player.text);

        }

        else  //若存在就更新值

        {

            WorkList.dictionary["Name"] = player.Name;

            WorkList.dictionary["FishId"] = player.fishId;

            WorkList.dictionary["Num"] = player.num;

            WorkList.dictionary["JinDu"] = player.jindu;

            WorkList.dictionary["RenWu"] = player.renwu;

            WorkList.dictionary["Jiang1"] = player.jiang1;

            WorkList.dictionary["Jiang2"] = player.jiang2;

            WorkList.dictionary["Week"] = player.week;

        }

        //找到当前路径

        FileInfo file = new FileInfo(filePath);

        //判断有没有文件,有则打开文件,没有创建后打开文件

        sw = file.CreateText();

        //ToJson接口将你的列表类传进去,并自动转换为string类型

        string json = JsonMapper.ToJson(WorkList.dictionary);

        //将转换好的字符串存进文件,

        sw.WriteLine(json);

        //注意释放资源

        sw.Close();

        sw.Dispose();

        AssetDatabase.Refresh();

        Debug.Log("保存进度成功");

    }

    //需要修改任务或保存任务进度的时候调用

    /// <summary>

    /// 读取保存数据的方法

    /// </summary>

    public Work Loadwork01()

    {

        //调试用的  //Debug.Log(1);

        Work work = new Work();

        //TextAsset该类是用来读取配置文件的

        TextAsset asset = Resources.Load("work" + GameData.Instance.userName) as TextAsset;

        if (!asset)  //读不到就退出此方法

            return null;

        string strdata = asset.text;

        jsdata3 = JsonMapper.ToObject(strdata);

        work.Name = (string)jsdata3[0];

        work.fishId = (string)jsdata3[1];

        work.num = (string)jsdata3[2];

        work.jindu = (string)jsdata3[3];

        work.renwu = (string)jsdata3[4];

        work.jiang1 = (string)jsdata3[5];

        work.jiang2 = (string)jsdata3[6];

        work.week = (string)jsdata3[7];

        //在这里循环输出表示读到了数据,,即此数据可以使用了

        Debug.Log("今天" + weekdays[Convert.ToInt32(DateTime.Now.DayOfWeek)]);

        return work;

    }

    /// <summary>

    /// 修改数据  切记必须new出来一个类  否则会报空引用的错误

    /// </summary>

    /// <param name="jindu"></param>

    public void SaveJinDu(string jindu)

    {

        Work w = new Work();

        w.Name = "绿鳄鱼|紫水母|蓝鲨鱼|老鼠鱼|灯笼鱼";

        w.fishId = "1|2|3|4|5";//

        w.num = "100|80|60|40|20";

        w.jindu = jindu;

        w.renwu = "1|2|3|4|5";

        w.jiang1 = "25|20|15|10|5";

        w.jiang2 = "5|10|15|20|25";

        w.week = DateTime.Now.DayOfWeek.ToString();

        Save(w);

    }

}

上一篇 下一篇

猜你喜欢

热点阅读