赞
踩
参考查阅官方教程:Dictionary, JSON and Streaming Assets
官方手册:JSON Serialization
使用主要API:JsonUtility
Unity序列化: Unity 序列化(Serialize)介绍、用途
下面两个实例工程的Github: Github Unity JSON Data Editor
JSON(JavaScript Object Notation) 常用于网络通信的数据交换还有打包、解压数据。在Unity就内嵌了处理JSON数据的类(UnityEngine.JsonUtility)。
因为不支持Dictionary(字典类型),但支持类或结构体,所以可以把字典封装到类或结构体内来使用。
方法名 | 签名 | 使用 |
---|---|---|
FromJson | public static T FromJson(string json) | 将json格式的字符串,转化成T类型数据,并返回T类型数据。 |
FromJsonOverwrite | public static void FromJsonOverwrite(string json, object objectToOverwrite) | 如果你已经有需要转换成的类对象,可以直接传入该对象作为赋值对象,省了新开数据对象。 |
ToJson | public static string ToJson(object obj, bool prettyPrint) | 第二个参数可以省略,等价于赋值false。将obj类型解析成Json数据字符串,如果第二个参数为true,这个字符串就会格式化,看起来舒服一点。 |
可以用FromJsonOverwrite来给已有对象添加数据,这些数据是JSON里面包含的字段,对于其他原有对象数据不会做任何修改。而如果用FromJson就会完全覆盖。
///
/// MyData.cs
///
[System.Serializable]
public class MyData
{
public MyDataItem[] items;
}
[System.Serializable]
public class MyDataItem
{
public string key;
public string value;
}
///
/// JsonDataManager
///
using UnityEngine;
using System.IO;
using UnityEngine.UI;
public class JsonDataManager : MonoBehaviour
{
public Text showText;
//文件名,在Assets/StreamingAssets目录下,如:myData.json。
public string fileName;
private MyData loadedData;
private void Start()
{
LoadJsonDateText();
showAllJsonData();
}
public void LoadJsonDateText()
{
//获取文件路径。
string filePath = Path.Combine(Application.streamingAssetsPath, fileName);
if (File.Exists(filePath)) //如果该文件存在。
{
string dataAsJson = File.ReadAllText(filePath); //读取所有数据送到json格式的字符串里面。
//直接赋值。FromJson
loadedData = JsonUtility.FromJson<MyData>(dataAsJson);
//使用已有对象,添加值。FromJsonOverwrite
//loadedData = new MyData();
//JsonUtility.FromJsonOverwrite(dataAsJson, loadedData);
Debug.Log("Data loaded, dictionary contains: " + loadedData.items.Length + " entries");
}
else
Debug.LogError("Cannot find file! Make sure file in \"Assets/StreamingAssets\" path. And file suffix should be \".json \"");
}
public void showAllJsonData()
{
showText.text = "";
for (int i = 0; i < loadedData.items.Length; ++i)
{
showText.text += loadedData.items[i].key + " : " + loadedData.items[i].value +"\n";
}
}
}
///
/// MyData.cs
///
[System.Serializable]
public class MyData
{
public MyDataItem[] items;
}
[System.Serializable]
public class MyDataItem
{
public string key;
public string value;
}
///
/// JsonDataEditor.cs
///
using UnityEngine;
using UnityEditor;
using System.IO;
public class JsonDataEditor : EditorWindow
{
public MyData myData;
private bool isPrettyPrint = false;
private string filePath = "";
[MenuItem("Window/Json Data Editor")]
static void Init()
{
GetWindow(typeof(JsonDataEditor)).Show();
}
private void OnGUI()
{
#region LoadData Button & CreateData Button & PrettyPrint Toggle & SaveData Button
EditorGUILayout.Space();
if (GUILayout.Button("Load json data"))
LoadJsonData();
EditorGUILayout.Space();
if (GUILayout.Button("Create json data"))
CreateNewData();
EditorGUILayout.Space();
GUILayout.BeginHorizontal();
isPrettyPrint = GUILayout.Toggle(isPrettyPrint, "Pretty Print");
if (GUILayout.Button("Save data"))
SaveJsonData();
GUILayout.EndHorizontal();
#endregion
EditorGUILayout.Space();
#region Handle Data Box
GUILayout.BeginVertical("Box");
if (myData != null)
{
GUILayout.Label(filePath);
//序列化myData对象,让它可以显示在编辑窗口。
SerializedObject serializedObject = new SerializedObject(this);
SerializedProperty serializedProperty = serializedObject.FindProperty("myData");
EditorGUILayout.PropertyField(serializedProperty, true);
serializedObject.ApplyModifiedProperties(); //应用修改的属性
}
GUILayout.EndVertical();
#endregion
}
private void LoadJsonData()
{
//使用系统文件打开窗口。
filePath = EditorUtility.OpenFilePanel("Select json data file", Application.streamingAssetsPath, "json");
if (!string.IsNullOrEmpty(filePath))
{
string dataAsJson = File.ReadAllText(filePath); //读取文件
myData = JsonUtility.FromJson<MyData>(dataAsJson); //反序列化JSON
}
}
private void SaveJsonData()
{
//使用系统文件保存窗口。
filePath = EditorUtility.SaveFilePanel("Save json data file", Application.streamingAssetsPath, "", "json");
if (!string.IsNullOrEmpty(filePath))
{
string dataAsJson = JsonUtility.ToJson(myData, isPrettyPrint); //序列化JSON
File.WriteAllText(filePath, dataAsJson); //写入文件
}
}
private void CreateNewData()
{
filePath = "";
myData = new MyData();
}
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。