国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

Unity學(xué)習(xí)筆記--數(shù)據(jù)持久化Json

這篇具有很好參考價(jià)值的文章主要介紹了Unity學(xué)習(xí)筆記--數(shù)據(jù)持久化Json。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。

JSON相關(guān)

json是國際通用語言,可以跨平臺(tái)(游戲,軟件,網(wǎng)頁,不同OS)使用,

json語法較為簡單,使用更廣泛。json使用鍵值對(duì)來存儲(chǔ)。

Unity學(xué)習(xí)筆記--數(shù)據(jù)持久化Json

認(rèn)識(shí)json文件

//注意字典類型存儲(chǔ)時(shí),鍵是以string類型存儲(chǔ)的 需要添加“”

{
  "name": "TonyChang",
  "age":21,
  "sex":true,
  "Float": 2.5,
  "arrarys":[1,5,9],
  "friends": [{"name": "Tom","age":21, "sex":true,"Float": 2.8},
    {"name": "Peter","age":17, "sex":true,"Float": 3.5},
    {"name": "Jack","age":25, "sex":true,"Float": 5.0}
  ],
  "university": {"address": "唐山","province":"河北"},
  "dic": {"1":"125","2": 911}, 
  "son": null
}

Excel轉(zhuǎn)換為JSON文件:

使用網(wǎng)站來轉(zhuǎn)換:bejson

挖坑-----》開發(fā)一個(gè)工具,使各種類型存儲(chǔ)文件進(jìn)行轉(zhuǎn)換。

Json的讀寫:

  1. jsonUtlity中的使用:

    string jisonStr = JsonUtility.ToJson(_writer);
    File.WriteAllText(Application.persistentDataPath+"/DemoJson1.json",jisonStr);
    

    轉(zhuǎn)儲(chǔ)為JSON時(shí)要點(diǎn):

    1. float存儲(chǔ)時(shí)看起來會(huì)有一些誤差
    2. 自定義類序列化要添加序列化特性[System.Serializable]
    3. 想要序列化私有變量,需要添加序列化特性[SerializeField]
    4. JsonUtility不支持字典
    5. JsonUtility存儲(chǔ)對(duì)象時(shí)候不會(huì)為null是默認(rèn)值的數(shù)值

完整的類

using System;
using System.Collections.Generic;
using System.IO;
using UnityEngine;

namespace Building.JSON
{
    public class Writer
    {
        public string name;
        public int age;
        public bool sex;
        [SerializeField]
        protected float numberF = 3.15f;
        [SerializeField]
        private double numberD = 6.15;
        public List<int> Array;
        public IDCard idcard;
    }

    [Serializable]
    public class IDCard
    {
        public int stu_id;
        public int cl_id;
    }
    public class JsonPractice:MonoBehaviour
    {
        private Writer _writer;

        private void Awake()
        {
            _writer = new Writer();
            _writer.name = "TonyCode";
            _writer.sex = true;
            _writer.age = 21;
            _writer.Array = new List<int>() {1, 2, 3, 4, 5};
            _writer.idcard = new IDCard();
            _writer.idcard.cl_id = 256;
            _writer.idcard.cl_id = 206;
            string jisonStr = JsonUtility.ToJson(_writer);
            File.WriteAllText(Application.persistentDataPath+"/DemoJson1.json",jisonStr);
            print(Application.persistentDataPath);
        }
    }
}

Unity學(xué)習(xí)筆記--數(shù)據(jù)持久化Json

存儲(chǔ)生成的json文件內(nèi)容。

反序列化:

string jsonStrRead = File.ReadAllText(Application.persistentDataPath + "/DemoJson1.json");
//反序列化 
Writer writer01 = JsonUtility.FromJson<Writer>(jsonStrRead);
Writer writer02=JsonUtility.FromJson(jsonStrRead,typeof(Writer)) as Writer;

注意:使用JsonUtility.FromJson進(jìn)行反序列化時(shí)候接受對(duì)象是一個(gè)對(duì)象,不能是一個(gè)數(shù)據(jù)集合來接受Json中存儲(chǔ)的數(shù)據(jù)內(nèi)容。并且json的編碼格式必須為UTF-8。

JSON文件的寫入與讀取,本質(zhì)是對(duì)text文本的讀寫,所以會(huì)調(diào)用File類中對(duì)text文本相關(guān)的方法。

先將類轉(zhuǎn)換為string字符串(Json文件格式的字符串),然后調(diào)用文件方法進(jìn)行讀寫。

2.LitJson(第三方開發(fā)的工具包)

 //使用LitJson存儲(chǔ)
 string jsonStr2=JsonMapper.ToJson(_writer);
 File.WriteAllText(Application.persistentDataPath+"/DemoJson2.json",jsonStr);

注意:(區(qū)別JsonUtility)

  • 不能序列化private變量;
  • 對(duì)于自定義類不需要添加序列化特性就可以進(jìn)行序列化;
  • 支持字典類型,建議字典類型的鍵的類型為“string”;
  • 可以準(zhǔn)確保存null類型;
//使用LitJson讀(反序列化)
string jsonStrRead3 = File.ReadAllText(Application.persistentDataPath + "/DemoJson2.json");
//方法一
JsonData data = JsonMapper.ToObject(jsonStrRead3);
print(data["name"]);
//方法二 (使用較多)
Writer writer03= JsonMapper.ToObject<Writer>(jsonStrRead3);

讀取數(shù)據(jù)集合:

//數(shù)據(jù)集合中元素類型
public class RoleInfo
{
    public int hp;
    public int speed;
    public int volume;
    public string resName;
    public int scale;
}
//讀取數(shù)據(jù)集合
 string jsonStrRoleList = File.ReadAllText(Application.persistentDataPath + "/RoleInfo.json");
 List<RoleInfo> roleInfos = JsonMapper.ToObject<List<RoleInfo>>(jsonStrRoleList);
 foreach (var roleInfo in roleInfos )
 {
 	print(roleInfo.hp+"   "+roleInfo.speed+"   "+roleInfo.resName);
 }

RoleInfo.json內(nèi)容

[
{"hp":4,"speed":6,"volume":5,"resName":"Airplane/Airplane1","scale":15},
{"hp":3,"speed":7,"volume":4,"resName":"Airplane/Airplane2","scale":15},
{"hp":2,"speed":8,"volume":3,"resName":"Airplane/Airplane3","scale":15},
{"hp":10,"speed":3,"volume":10,"resName":"Airplane/Airplane4","scale":6},
{"hp":6,"speed":5,"volume":7,"resName":"Airplane/Airplane5","scale":10}
]

讀取字典:

 //讀取字典
 //注意字典元素內(nèi)容最后一個(gè)不應(yīng)當(dāng)添加逗號(hào)
 string jsonStrDic = File.ReadAllText(Application.persistentDataPath + "/JsonDic.json");
 Dictionary<string, int> games = JsonMapper.ToObject<Dictionary<string, int>>(jsonStrDic);
 foreach (KeyValuePair<string,int> kv in games)
 {
	 print(kv.Key+" "+kv.Value);
 }

JsonDic.json中內(nèi)容:

{
   "Tony": 100,
   "Jack": 125,
   "Pony": 156,
   "Tom": 126
}

注意:

  1. litJson支持字典存儲(chǔ),字典鍵的類型為string
  2. 自定義類結(jié)構(gòu)一定要有無參構(gòu)造函數(shù)
  3. LitJson可以直接讀取數(shù)據(jù)集合
  4. LitJson讀取字典元素時(shí)候 字典內(nèi)容最后一個(gè)不可以加逗號(hào)(可能反序列化失?。?/strong>

對(duì)比JsonUtility和JsonLit二者使用:

相同:

  1. 二者都是對(duì)json文件進(jìn)行序列化與反序列化的工具類
  2. 二者都是靜態(tài)調(diào)用,使用其中的方法
  3. json的格式必須為UTF-8格式

區(qū)別:LitJson對(duì)比JsonUtility文章來源地址http://www.zghlxwxcb.cn/news/detail-747820.html

  1. 對(duì)于空對(duì)象,LitJson對(duì)可以存儲(chǔ)null類型,而后者只存儲(chǔ)對(duì)應(yīng)的默認(rèn)數(shù)據(jù)數(shù)據(jù)值
  2. LItJson支持?jǐn)?shù)據(jù)集合的讀取,而后者需要將其轉(zhuǎn)換為對(duì)象內(nèi)部的數(shù)組才可以讀取,讀取存儲(chǔ)結(jié)果為對(duì)象類型,而非數(shù)據(jù)集類型。
  3. LitJson支持對(duì)字典的存取,字典的鍵的類型為string
  4. LitJson存儲(chǔ)茲定于數(shù)據(jù)類型時(shí)候無需添加序列化特性,而后者需要
  5. LitJson不支持對(duì)priivate數(shù)據(jù)內(nèi)容的序列化,后者使用時(shí)候添加序列化特性則可以完成存儲(chǔ)
  6. LitJson要求自定義數(shù)據(jù)類型必須有無參構(gòu)造,而JsonUtility則不需要

Json讀存的工具類:

using System.IO;
using LitJson;
using UnityEngine;

namespace Building.JSON
{
    public enum SaveJsonType
    {
        JsonUtility,
        LitJson
    }
    public class JsonManager
    {
        private static JsonManager instance=new JsonManager();
        public static JsonManager Instance => instance;

        private JsonManager()
        {
            
        }
        /// <summary>
        ///  存儲(chǔ)Json方法
        /// </summary>
        /// <param name="data">要存儲(chǔ)的數(shù)據(jù)類</param>
        /// <param name="FileName">存儲(chǔ)文件名稱</param>
        /// <param name="type">存儲(chǔ)方法</param>
        public void SaveJson(object data, string FileName,SaveJsonType type=SaveJsonType.LitJson)
        {
            //確定路徑
            string path = Application.persistentDataPath + "/"+FileName+ ".json";
            string jsonStr="";
            switch (type)
            {
                case SaveJsonType.LitJson:
                    jsonStr = JsonMapper.ToJson(data);
                    break;
                case SaveJsonType.JsonUtility:
                    jsonStr = JsonUtility.ToJson(data);
                    break;
            }
           //寫入文件
           File.WriteAllText(path,jsonStr);
        }
        
        /// <summary>
        /// 讀取json方法
        /// </summary>
        /// <param name="FileName">讀取json的名稱</param>
        /// <param name="type">讀取方式</param>
        /// <typeparam name="T">讀取json文件中數(shù)據(jù)類型</typeparam>
        /// <returns></returns>
        public T LoadJson<T>(string FileName, SaveJsonType type = SaveJsonType.LitJson) where T : new()
        {
            //確定路徑
            string path = Application.persistentDataPath + "/"+FileName+ ".json";
            if (!File.Exists(path))
            {
                path=Application.streamingAssetsPath+"/"+FileName+".json";
                if (!File.Exists(path))
                {
                    return new T();
                }
            }
            string jsonStrRead=File.ReadAllText(path);
            T data = default(T);
            switch (type)
            {
                case SaveJsonType.LitJson:
                    data=JsonMapper.ToObject<T>(jsonStrRead);
                    break;
                case SaveJsonType.JsonUtility:
                    data= JsonUtility.FromJson<T>(jsonStrRead);
                    break;
            }
            return data;
        }
    }
}

到了這里,關(guān)于Unity學(xué)習(xí)筆記--數(shù)據(jù)持久化Json的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • 【Unity】二進(jìn)制文件 數(shù)據(jù)持久化(修改版)【個(gè)人復(fù)習(xí)筆記/有不足之處歡迎斧正/侵刪】

    ???????? 變量的本質(zhì)都是二進(jìn)制 ,在內(nèi)存中都以字節(jié)的形式存儲(chǔ)著,通過sizeof方法可以看到常用變量類型占用的字節(jié)空間長度( 1byte = 8bit,1bit(位)不是0就是1 ) ? ? ? ? 二進(jìn)制文件讀寫的本質(zhì): 將各類型變量轉(zhuǎn)換為字節(jié)數(shù)組,將字節(jié)數(shù)組直接存儲(chǔ)到文件中 ,不僅可以節(jié)

    2024年04月17日
    瀏覽(25)
  • 「學(xué)習(xí)筆記」可持久化線段樹

    「學(xué)習(xí)筆記」可持久化線段樹

    可持久化數(shù)據(jù)結(jié)構(gòu) (Persistent data structure) 總是可以保留每一個(gè)歷史版本,并且支持操作的不可變特性 (immutable)。 主席樹全稱是可持久化權(quán)值線段樹,給定 (n) 個(gè)整數(shù)構(gòu)成的序列 (a) ,將對(duì)于指定的閉區(qū)間 (left[l, rright]) 查詢其區(qū)間內(nèi)的第 (k) 小值。 圖片來自 (texttt{OI-w

    2024年02月02日
    瀏覽(23)
  • ?「學(xué)習(xí)筆記」可持久化線段樹?

    ?「學(xué)習(xí)筆記」可持久化線段樹?

    可持久化數(shù)據(jù)結(jié)構(gòu) (Persistent data structure) 總是可以保留每一個(gè)歷史版本,并且支持操作的不可變特性 (immutable)。 主席樹全稱是可持久化權(quán)值線段樹,給定 (n) 個(gè)整數(shù)構(gòu)成的序列 (a) ,將對(duì)于指定的閉區(qū)間 (left[l, rright]) 查詢其區(qū)間內(nèi)的第 (k) 小值。 圖片來自 (texttt{OI-w

    2024年02月02日
    瀏覽(18)
  • 「學(xué)習(xí)筆記」可持久化線段樹?

    「學(xué)習(xí)筆記」可持久化線段樹?

    可持久化數(shù)據(jù)結(jié)構(gòu) (Persistent data structure) 總是可以保留每一個(gè)歷史版本,并且支持操作的不可變特性 (immutable)。 主席樹全稱是可持久化權(quán)值線段樹,給定 (n) 個(gè)整數(shù)構(gòu)成的序列 (a) ,將對(duì)于指定的閉區(qū)間 (left[l, rright]) 查詢其區(qū)間內(nèi)的第 (k) 小值。 圖片來自 (texttt{OI-w

    2024年02月02日
    瀏覽(20)
  • Unity數(shù)據(jù)持久化之PlayerPrefs

    Unity數(shù)據(jù)持久化之PlayerPrefs

    什么是數(shù)據(jù)持久化 數(shù)據(jù)持久化就是將內(nèi)存中的數(shù)據(jù)模型轉(zhuǎn)換為存儲(chǔ)模型,以及將存儲(chǔ)模型轉(zhuǎn)換為內(nèi)存中的數(shù)據(jù)模型的統(tǒng)稱。即將游戲數(shù)據(jù)存儲(chǔ)到硬盤,硬盤中數(shù)據(jù)讀取到游戲中,也就是傳統(tǒng)意義上的存盤。 PlayerPrefs是什么 是 Unity 提供的可以用于存儲(chǔ)讀取玩家數(shù)據(jù)的公共類

    2024年02月19日
    瀏覽(19)
  • Unity之?dāng)?shù)據(jù)持久化——Json

    JavaScript對(duì)象簡譜(JavaScript Object Notation) json是國際通用的一種輕量級(jí)的數(shù)據(jù)交換格式,主要在網(wǎng)絡(luò)通訊中用于傳輸數(shù)據(jù),或本地?cái)?shù)據(jù)存儲(chǔ)和讀取,易于人閱讀和編寫,同時(shí)也易于機(jī)器解析和生成,并有效地提升網(wǎng)絡(luò)傳輸效率 游戲中可以把游戲數(shù)據(jù)按照J(rèn)son的格式標(biāo)準(zhǔn)存儲(chǔ)在

    2023年04月20日
    瀏覽(20)
  • Unity PlayerPrefs 持久化數(shù)據(jù)存在哪

    Unity PlayerPrefs 持久化數(shù)據(jù)存在哪

    在游戲開發(fā)的過程中,我們經(jīng)常需要存檔相關(guān)的東西,稱為數(shù)據(jù)的持久化。PlayerPrefs 就是Unity提供的用于本地?cái)?shù)據(jù)持久化保存與讀取的類。 PlayerPrefs會(huì)以鍵值對(duì)的方式存儲(chǔ)在本地的注冊(cè)表中。 1.存儲(chǔ)數(shù)據(jù) 2.獲取數(shù)據(jù) 3.刪除數(shù)據(jù) 這些數(shù)據(jù)會(huì)存儲(chǔ)在注冊(cè)表中,打開注冊(cè)表就能查看

    2024年02月16日
    瀏覽(26)
  • 【unity之?dāng)?shù)據(jù)持久化】-Unity公共類PlayerPrefs

    【unity之?dāng)?shù)據(jù)持久化】-Unity公共類PlayerPrefs

    ?????個(gè)人主頁 :@元宇宙-秩沅 ????? hallo 歡迎 點(diǎn)贊?? 收藏? 留言?? 加關(guān)注?! ????? 本文由 秩沅 原創(chuàng) ????? 收錄于專欄 : unity數(shù)據(jù)存儲(chǔ) API大全圖解 windows平臺(tái)存儲(chǔ)路徑 HKCUSoftware[公司名稱][產(chǎn)品名稱] 項(xiàng)下的注冊(cè)表中 公司和產(chǎn)品名稱是 在“Project Settings”中設(shè)

    2024年02月04日
    瀏覽(58)
  • 【Unity】數(shù)據(jù)持久化路徑Application.persistentDataPath

    【Unity】數(shù)據(jù)持久化路徑Application.persistentDataPath

    今天突然想到這個(gè)路徑Application.persistentDataPath,熱更的重要路徑,該文件夾可讀可寫,在移動(dòng)端唯一一個(gè)可讀寫操作的文件夾。 移動(dòng)端可以將本地的資源(資源MD5值配置表)等一些文件放到StreamingAssets文件夾下,通過Copy到persistentDataPath下與服務(wù)器的版本文件配置表作比對(duì),

    2023年04月10日
    瀏覽(27)
  • 【unity數(shù)據(jù)持久化】XML數(shù)據(jù)管理器知識(shí)點(diǎn)

    【unity數(shù)據(jù)持久化】XML數(shù)據(jù)管理器知識(shí)點(diǎn)

    ?????個(gè)人主頁 :@元宇宙-秩沅 ????? hallo 歡迎 點(diǎn)贊?? 收藏? 留言?? 加關(guān)注?! ????? 本文由 秩沅 原創(chuàng) ????? 收錄于專欄 :Unity基礎(chǔ)實(shí)戰(zhàn) XML是什么 XML(Extensible Markup Language)是一種類似于 HTML,但是沒有使用預(yù)定義標(biāo)記的語言。因此,可以根據(jù)自己的設(shè)計(jì)需求

    2024年02月11日
    瀏覽(56)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包