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

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

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

XML相關(guān)

Xml是可拓展標(biāo)記語(yǔ)言,一種文件格式。我們使用xml來完成對(duì)數(shù)據(jù)持久化的存儲(chǔ)。等待我們有一程序運(yùn)行結(jié)束之后,將內(nèi)存中的數(shù)據(jù)進(jìn)行保存,(保存在硬盤/服務(wù)器)實(shí)現(xiàn)對(duì)數(shù)據(jù)的持久化存儲(chǔ)。

xml文件的讀取和保存以及修改

要點(diǎn):

  1. XMl文件的加載

  2. XML文件節(jié)點(diǎn)的查找訪問

  3. XML文件節(jié)點(diǎn)內(nèi)容的讀取 (InnerText還是Attributes["id"].Value 形式訪問)

    代碼中有詳細(xì)注釋!可供參考對(duì)比學(xué)習(xí)!

using System.IO;
using System.Xml;
using UnityEngine;

namespace Building.XML
{
    public class LoadXMLFile:MonoBehaviour
    {
        private void Start()
        {
            //得到xml文件
            XmlDocument xmlFile = new XmlDocument();
            //通過加載text格式進(jìn)行解析成xml形式進(jìn)行獲取
            //TextAsset textAsset = Resources.Load<TextAsset>("Text");
            // xmlFile.LoadXml(textAsset.text);
            //通過路徑進(jìn)行加載
            xmlFile.Load(Application.streamingAssetsPath+"/Text.xml");
            
            //讀取xml中節(jié)點(diǎn)
            XmlNode root = xmlFile.SelectSingleNode("PlayerInfo");
            XmlNode nodeName = root.SelectSingleNode("Name");
            XmlNode nodeList = root.SelectSingleNode("Item");
            //獲取自定義版本的數(shù)據(jù)結(jié)構(gòu)類型
            print(nodeList.Attributes["id"].Value);
            print(nodeList.Attributes["size"].Value);
            //或者
            print(nodeList.Attributes.GetNamedItem("id").Value);
            print(nodeList.Attributes.GetNamedItem("size").Value);
            
            //直接獲取數(shù)組中的元素
            XmlNode tempNodeList1 = root.SelectSingleNode("ItemList1");
            XmlNodeList xmlNodeList1 = tempNodeList1.SelectNodes("Item");
            //找出List中所有的節(jié)點(diǎn)  打印節(jié)點(diǎn)組中的 id size節(jié)點(diǎn)的InnerText
            //var 類型推斷不出來 XmlNode類型
            foreach (XmlNode item in xmlNodeList1)
            {
                print(item.Name);
                print(item.SelectSingleNode("id").InnerText);  
            /* <Item>
                <id>2003</id>>   通過InnerText來訪問<> <>中間的內(nèi)容 
                <size>17.5</size>>
               </Item>>*/
                print(item.SelectSingleNode("size").InnerText);
            }

            for (int i = 0; i < xmlNodeList1.Count; i++)
            {
                print(xmlNodeList1[i].Name);
                print(xmlNodeList1[i].SelectSingleNode("id").InnerText);
                print(xmlNodeList1[i].SelectSingleNode("size").InnerText);
            }
            
            //直接獲取數(shù)組中的元素 形式  innerText訪問還是獲取  Attributes["size"].Value 訪問數(shù)值
            //-------注意區(qū)分元素中是否還有子節(jié)點(diǎn) 根據(jù)是否有子節(jié)點(diǎn)來選擇獲取節(jié)點(diǎn)內(nèi)容
            XmlNode tempNodeList = root.SelectSingleNode("ItemList");
            XmlNodeList xmlNodeList = tempNodeList.SelectNodes("Item");
            //找出List中所有的節(jié)點(diǎn)  打印節(jié)點(diǎn)組中的 id size節(jié)點(diǎn)的InnerText
            //var 類型推斷不出來 XmlNode類型
            foreach (XmlNode item in xmlNodeList)
            {
                print(item.Name);
                print(item.Attributes["id"].InnerText);
                print(item.Attributes["size"].Value);
            }

            for (int i = 0; i < xmlNodeList.Count; i++)
            {
                print(xmlNodeList[i].Name);
                print(xmlNodeList[i].Attributes["id"].Value); //<Item id="2011" size="15.5"/>
                                                              //單行內(nèi)嵌的 通過Attributs["name"].Value訪問
                print(xmlNodeList[i].Attributes["size"].Value);
            }
           
            //============================讀================
            //==================xml存儲(chǔ)的路徑
            // 1.Resources 可讀 不可寫 打包后找不到  ×
            // 2.Application.streamingAssetsPath 可讀 PC端可寫 找得到  ×
            // 3.Application.dataPath 打包后找不到  ×
            // 4.Application.persistentDataPath 可讀可寫找得到   √

            string path = Application.streamingAssetsPath + "/xmlSaveFile.xml";
            print(Application.persistentDataPath);
            
            //創(chuàng)建固定版本信息
            XmlDocument saveXmlFile = new XmlDocument();
            //文件格式聲明
            XmlDeclaration xmlDeclaration = saveXmlFile.CreateXmlDeclaration("1.0", "utf-8", "");
            saveXmlFile.AppendChild(xmlDeclaration);
            
            //添加根節(jié)點(diǎn)
            //這里以存儲(chǔ)班級(jí)信息為例子
            XmlElement classInfo =saveXmlFile.CreateElement("ClassInfo");
            saveXmlFile.AppendChild(classInfo);
            
            //創(chuàng)建子節(jié)點(diǎn)
            XmlElement teacher = saveXmlFile.CreateElement("teacher");
            classInfo.AppendChild(teacher);

            XmlElement teacherName = saveXmlFile.CreateElement("teacherName");
            teacherName.InnerText = "TonyChang";
            teacher.AppendChild(teacherName);
            
            XmlElement teacherId = saveXmlFile.CreateElement("teacherId");
            teacherId.InnerText = "3306";//設(shè)置teacherID名稱
            teacher.AppendChild(teacherId);

            //學(xué)生信息模塊
            XmlElement stusElement = saveXmlFile.CreateElement("Students");
            XmlElement stuEle;//學(xué)生信息
            for (int i = 0; i < 15; i++)
            {
                stuEle = saveXmlFile.CreateElement("Student");
                stuEle.SetAttribute("stuNo", (i + 1).ToString());
                stuEle.SetAttribute("age", 19.ToString());
                stusElement.AppendChild(stuEle);
            }
            //學(xué)生信息模塊添加到xml文件中
            classInfo.AppendChild(stusElement);
            
            //保存Xml文件
            saveXmlFile.Save(path);

            //----------------------XML的內(nèi)容修改------------------------
            if (File.Exists(path))
            {
                XmlDocument Xml1 = new XmlDocument();
                //加載到新建的的xml文件中
                Xml1.Load(path);
                
                //獲取要修改的文件節(jié)點(diǎn)
                XmlNode changeNode;
                changeNode = Xml1.SelectSingleNode("ClassInfo/teacher/teacherId");
                //將3306-->8888
                changeNode.InnerText = "8888";

                //---刪除(通過父節(jié)點(diǎn)刪除)
                XmlNode FatherNode = Xml1.SelectSingleNode("ClassInfo/teacher");
                FatherNode.RemoveChild(changeNode);

                //---添加新的節(jié)點(diǎn)
                XmlNode changedNode=Xml1.CreateElement("teacherId");
                changedNode.InnerText = "8888";
                FatherNode.AppendChild(changedNode);
                
                //修改了記得保存
                Xml1.Save(path);
            }
        }
    }
}

textXML文件

<?xml version="1.0" encoding="utf-8"?>
<!--注釋內(nèi)容-->
<PlayerInfo >
    <name>TonyChang</name>
    <age>18</age>
    <height>175.5</height>>
    <Item id="1997" size="12.5"/>
    <Item1>
        <id>1998</id>>
        <size>12.25</size>
    </Item1>>
    <ItemList1>
        <!--屬性
        和元素節(jié)點(diǎn)的區(qū)別?
        表現(xiàn)形式不同的同種意義
        -->
        <Item>
            <id>2002</id>>
            <size>16.5</size>>
        </Item>>
        <Item>
            <id>2003</id>>
            <size>17.5</size>>
        </Item>>
        <Item>
            <id>2004</id>>
            <size>18.5</size>>
        </Item>>
    </ItemList1>
    <ItemList>
        <!--屬性
        和元素節(jié)點(diǎn)的區(qū)別?
        表現(xiàn)形式不同的同種意義
        -->
        <Item id="2011" size="15.5"/>
        <Item id="2012" size="16.5"/>
        <Item id="2013" size="17.5"/>
    </ItemList>
</PlayerInfo>

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

文件格式如圖所示;

生成新建的“xmlSaveFile.xml”文件內(nèi)容

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

XML的序列化與反序列化

序列化:

using System.Collections.Generic;
using System.IO;
using System.Xml.Serialization;
using UnityEngine;
public class Test
{
    public int testPub = 10;
    private int testPri = 5;
    protected int testProtect = 12;
    internal int testInter = 15;

    public string testStr = "TonyChang";
    //屬性
    public int testPro { get; set; }
    //數(shù)組
    public int[] arrayInt=new []{2,4,6,8};

    //自定義類
    public Test2 Test2 = new Test2();
    
    //list
    //更改數(shù)組屬性名字
    [XmlArray("IntList")]
    [XmlArrayItem("TCInt")]
    public List<int> listInt=new List<int>(){1,5,7,8,9};

    //不支持字典
   // public Dictionary<int, string> testDic = new Dictionary<int, string>() {{1, "Jerry"},{2,"Tom"}};
}

public class Test2
{
    //屬性標(biāo)簽
    [XmlAttribute("Test2")] public int xmlTest = 5;
    [XmlAttribute()] public float test2Float = 6.6f;
    [XmlAttribute()] public bool aryUok = true;
}
/// <summary>
/// XML的序列化與反序列化
/// </summary>
public class XMLDemo : MonoBehaviour
{
    
    private void Start()
    {
        //要存儲(chǔ)為XML文件的文件流
        Test test = new Test();
        string path = Application.persistentDataPath + "/XMLTest01.xml";
        print(path);
        //流語(yǔ)句
        //using加載文件 文件加載結(jié)束之后則會(huì)自動(dòng)關(guān)閉
        using(StreamWriter stream = new StreamWriter(path) )
        {
            XmlSerializer xmlSerializer = new XmlSerializer(typeof(Test));
            //將XML文件序列化 寫入流中
            //流根據(jù)配置的寫入地址進(jìn)行設(shè)置
            xmlSerializer.Serialize(stream,test);
        }
    }
}
  • 只能進(jìn)行公共類型變量的存儲(chǔ)
  • 不支持字典存儲(chǔ)

對(duì)應(yīng)生成的XML文件如下圖所示:

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

反序列化:文章來源地址http://www.zghlxwxcb.cn/news/detail-746905.html

//反序列化
//判斷是否存在要讀取的XML文件
if (File.Exists(path))
{
using (StringReader reader = new StringReader(path))
{
XmlSerializer xmlSerializer = new XmlSerializer(typeof(Test));
Test testReaded=xmlSerializer.Deserialize(reader) as Test;
}
}
//tips:
//list對(duì)象中有默認(rèn)值時(shí)候,反序列化時(shí)候會(huì)把默認(rèn)值再次添加到list數(shù)組中
//可以理解為 當(dāng)前反序列化時(shí)候要考察各種變量的默認(rèn)值(初始值)
//如果過本身序列化時(shí)候會(huì)有new List(){1,2,3}之類的賦值操作
//則反序列化時(shí)會(huì)重新將1,2,3數(shù)值添加到list數(shù)組中,得到的
//反序列化結(jié)果中會(huì)有兩個(gè)1,2,3  1,2,3 內(nèi)容

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

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(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é)空間長(zhǎng)度( 1byte = 8bit,1bit(位)不是0就是1 ) ? ? ? ? 二進(jìn)制文件讀寫的本質(zhì): 將各類型變量轉(zhuǎn)換為字節(jié)數(shù)組,將字節(jié)數(shù)組直接存儲(chǔ)到文件中 ,不僅可以節(jié)

    2024年04月17日
    瀏覽(25)
  • Unity筆記:數(shù)據(jù)持久化的幾種方式

    主要方法: ScriptableObject PlayerPrefs JSON XML 數(shù)據(jù)庫(kù)(如Sqlite) PlayerPrefs 存儲(chǔ)的數(shù)據(jù)是 全局共享 的,它們存儲(chǔ)在用戶設(shè)備的本地存儲(chǔ)中,并且可以被應(yīng)用程序的所有部分訪問。這意味著,無(wú)論在哪個(gè)場(chǎng)景、哪個(gè)腳本中,只要是同一個(gè)應(yīng)用程序中的代碼,都可以讀取和修改 Playe

    2024年02月19日
    瀏覽(23)
  • 【Unity學(xué)習(xí)日記03】數(shù)據(jù)持久化

    【Unity學(xué)習(xí)日記03】數(shù)據(jù)持久化

    這一篇只能說寫了一部分,并沒有把Unity里數(shù)據(jù)持久化的操作講完整,之后可能是學(xué)到一點(diǎn)就記一點(diǎn)的模式。 數(shù)據(jù)持久化就是將內(nèi)存中的數(shù)據(jù)模型轉(zhuǎn)換為存儲(chǔ)模型,以及將存儲(chǔ)模型轉(zhuǎn)換為內(nèi)存中的數(shù)據(jù)模型的統(tǒng)稱。 人話版:將游戲數(shù)據(jù)存儲(chǔ)到硬盤,硬盤中數(shù)據(jù)讀取到游戲中,

    2024年02月12日
    瀏覽(17)
  • 「學(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ì)象簡(jiǎn)譜(JavaScript Object Notation) json是國(guó)際通用的一種輕量級(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è)人主頁(yè) :@元宇宙-秩沅 ????? 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)

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

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

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

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

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包