国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

【轉載】Unity的Json解析<一>--讀取Json文件

2019-11-08 02:29:06
字體:
來源:轉載
供稿:網友

本文章由cartzhang編寫,

https://unity3d.com/cn/unity/whats-new/unity-5.3

https://blogs.unity3d.com/cn/2015/12/08/unity-5-3-all-new-features-and-more-platforms/

Json的在線編輯

Json parser :http://json.parser.online.fr/ Json在線編輯:http://www.kjson.com/jsoneditor/?f=1

第二個是可以直接下載保存到本地的,此操作甚好??!

JsonUtility

先看看,在Unity中,我們在其Json工具類中,可用的函數,總共就5個,好少啊!不過,simple is better.

#region 程序集 UnityEngine, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null// H:/Unity/PRoject/JsonReadTest/Library/UnityAssemblies/UnityEngine.dll#endregionusing System;namespace UnityEngine{ // // 摘要: // /// // Utility functions for working with JSON data. // /// public static class JsonUtility { // // 摘要: // /// // Create an object from its JSON representation. // /// // // 參數: // json: // The JSON representation of the object. // // type: // The type of object represented by the JSON. // // 返回結果: // /// // An instance of the object. // /// [WrapperlessIcall] public static object FromJson(string json, Type type); public static T FromJson<T>(string json); // // 摘要: // /// // Overwrite data in an object by reading from its JSON representation. // /// // // 參數: // json: // The JSON representation of the object. // // objectToOverwrite: // The object that should be overwritten. [WrapperlessIcall] public static void FromJsonOverwrite(string json, object objectToOverwrite); // // 摘要: // /// // Generate a JSON representation of the public fields of an object. // /// // // 參數: // obj: // The object to convert to JSON form. // // prettyPrint: // If true, format the output for readability. If false, format the output for minimum // size. Default is false. // // 返回結果: // /// // The object's data in JSON format. // /// public static string ToJson(object obj); // // 摘要: // /// // Generate a JSON representation of the public fields of an object. // /// // // 參數: // obj: // The object to convert to JSON form. // // prettyPrint: // If true, format the output for readability. If false, format the output for minimum // size. Default is false. // // 返回結果: // /// // The object's data in JSON format. // /// [WrapperlessIcall] public static string ToJson(object obj, bool prettyPrint); }}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990

可以看到,主要就是創建對象的和變成Json格式的兩個類型。

準備工作

我們需要一個Json文件,這是主角啊,就是要讀取其內容啊。 好了。我隨便寫了個,命名為Test.json放到了我的./Assets/Resources/目錄下; Json文件內容如下:

{"gameName":"JSON Serializer Test","version":"1.0","isStereo":"false","isUseHardWare":"true","statusList":[{"name":"test","id":"1u702"}]}11

創建對象類

首先,我們需要創建一個對象類,用來存放從Json文本中讀取的內容。 給這個類命名為GameStatus.cs

using UnityEngine;using System;using System.Collections;[Serializable]public class GameStatus{ public string gameName; public string version; public bool isStereo; public bool isUseHardWare; public refencenes[] statusList;}[Serializable]public class refencenes{ public string name; public int id;}1234567891011121314151617181920212212345678910111213141516171819202122

需要一個讀取類

這個寫了一個靜態類,以后也可添加寫的內容 名字為LoadJson.cs

using UnityEngine;using System.Collections;using System.IO;using System.Runtime.Serialization.Formatters.Binary;public class LoadJson : MonoBehaviour{ public static GameStatus LoadJsonFromFile() { BinaryFormatter bf = new BinaryFormatter(); if (!File.Exists(application.dataPath + "/Resources/Test.json")) { return null; } StreamReader sr = new StreamReader(Application.dataPath + "/Resources/Test.json"); //FileStream file = File.Open(Application.dataPath + "/Test.json", FileMode.Open, Fileaccess.ReadWrite); //if (file.Length == 0) //{ // return null; //} //string json = (string)bf.Deserialize(file); //file.Close(); if (sr == null) { return null; } string json = sr.ReadToEnd(); if (json.Length > 0) { return JsonUtility.FromJson<GameStatus>(json); } return null; }}12345678910111213141516171819202122232425262728293031323334353637383940414243441234567891011121314151617181920212223242526272829303132333435363738394041424344

說明:代碼被注釋掉的部分,是從網上找的,解析Json為二進制格式,然后在反序列化為字符串,結果,可能是編碼格式問題,不能正確的反序列化,總是報錯。 我表示無奈,只好從寫實使用了StreamReader來處理。

調用

調用蠻簡單的,就是在Update中,使用L鍵來加載Json文件。 代碼如下:

using UnityEngine;using System.Collections;using System;using System.Runtime.Serialization.Formatters.Binary;using System.IO;public class ReadJson : MonoBehaviour{ void Update() { if(Input.GetKeyDown(KeyCode.S)) { //Save(); } if (Input.GetKeyDown(KeyCode.L)) { GameStatus status = LoadJson.LoadJsonFromFile(); Debug.Log(status); } }}1234567891011121314151617181920212223242512345678910111213141516171819202122232425

測試結果

在場景中,建立一個空對象,然后把ReadJson拖拽到對象上,運行,按下L鍵,就可以使用斷點查看,當然也后Log輸出。

結果如下圖: Unity視圖

Debug

說明

總體過程都很簡單。工程就不一一上傳。


若有問題,請隨時聯系! 非常感謝! –張偉


上一篇:STM32F4(USART+DMA+動態內存)

下一篇:指針

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 肇源县| 赞皇县| 普格县| 丹江口市| 永康市| 射洪县| 遵义县| 无为县| 汪清县| 永昌县| 肇州县| 广西| 双鸭山市| 沾化县| 泌阳县| 西畴县| 扬中市| 隆尧县| 新巴尔虎右旗| 吕梁市| 乌鲁木齐县| 高阳县| 富裕县| 长岛县| 安仁县| 泰州市| 河池市| 屏东市| 松溪县| 孟州市| 新闻| 宿州市| 乐清市| 鹰潭市| 榕江县| 横峰县| 莫力| 邮箱| 北碚区| 牟定县| 林甸县|