Unity JSON文件的写入(序列化)与读取(反序列化)、以及自定义JSON编辑器
发表于2018-01-05
JSON(JavaScript Object Notation) 常用于网络通信的数据交换还有打包、解压数据。在Unity就内嵌了处理JSON数据的类(UnityEngine.JsonUtility),本篇文章就和大家介绍下JSON文件的写入与读取以及自定义JSON编辑器的使用。
JsonUtility
- 可用于的类型
- MonoBehaviour的子类
- ScriptableObject的子类
- 带[Serializable]特性的类或结构体。
因为不支持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就会完全覆盖。
实例1. 读取.json文件数据,显示在场景中。
基本数据类 MyData
/// /// MyData.cs /// [System.Serializable] public class MyData { public MyDataItem[] items; } [System.Serializable] public class MyDataItem { public string key; public string value; }
JSON数据读取,显示在Text中。
/// /// 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"; } } }
测试结果
1. Unity中操作,如图。
2. myData.json的内容如下。
3. 运行游戏,显示如下。
实例2.自定义JSON编辑窗口
同上一个实例一样,基本数据类 MyData
/// /// MyData.cs /// [System.Serializable] public class MyData { public MyDataItem[] items; } [System.Serializable] public class MyDataItem { public string key; public string value; }
JSON数据编辑窗口类 JsonDataEditor,在菜单window->Json Data Editor。
/// /// 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(); } }
测试结果