您的位置:首页 > 移动开发 > Unity3D

Unity 使用JSON实现本地数据保存和读取

2017-09-21 21:22 836 查看
通过键值对的方式对游戏对象信息进行存储和读取,,,下面以一个人物对象为例,

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)
{
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();
}
}
}


注:刚刚有人评论,说这个代码,发布之后运行保存数据保存不上,后来查出是保存路径的问题,Resources属于Unity中的特殊文件夹,,,关于特殊文件夹的简介:http://blog.csdn.net/czhenya/article/details/78095273,,,在此对一楼博主的纠错表示感谢,,希望大家注意一下这个问题,,,

效果图:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: