当前位置:   article > 正文

Unity JSON文件的写入(序列化)与读取(反序列化)、以及自定义JSON编辑器_unity serializeproperty

unity serializeproperty

Unity JSON文件的写入(序列化)与读取(反序列化)

参考查阅官方教程: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)。


JsonUtility

  • 可用于的类型
    • MonoBehaviour的子类
    • ScriptableObject的子类
    • 带[Serializable]特性的结构体

因为不支持Dictionary(字典类型),但支持类或结构体,所以可以把字典封装到类或结构体内来使用。


三个静态方法

方法名签名使用
FromJsonpublic static T FromJson(string json)将json格式的字符串,转化成T类型数据,并返回T类型数据。
FromJsonOverwritepublic static void FromJsonOverwrite(string json, object objectToOverwrite)如果你已经有需要转换成的类对象,可以直接传入该对象作为赋值对象,省了新开数据对象。
ToJsonpublic 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;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

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
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54

测试结果

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;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

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

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98

测试结果

1. 打开自定义窗口。(Window->Json Data Editor)

2. 可以读取文件,创建新文件,可以选择是否格式化(ToJson函数第二个参数是否true),还可以保存文件。

3. 结合实例1还可以显示在场景画布。


声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/正经夜光杯/article/detail/794152
推荐阅读
相关标签
  

闽ICP备14008679号