亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在運行時保存和更新“.xml”文件?

如何在運行時保存和更新“.xml”文件?

C#
ABOUTYOU 2023-07-09 15:14:49
我正在開發一個游戲項目,我們將玩家收集的物品保存到 .xml 文件中,我們這樣做是為了跟蹤玩家的開發過程。例如,您要進入地牢殺死敵人。殺死敵人后,游戲會隨機掉落物品,如果您想將該物品添加到庫存中,只需單擊它即可。這就是問題所在。將新項目保存到 xml 文檔的功能工作正常,但如果我手動刷新 xml 頁面或重新啟動游戲,它只會添加收集的項目。(在這種情況下我們不希望)有什么方法或解決方法可以讓我在游戲運行時更新 xml 文檔并保存它?我嘗試使用“XmlTextWriter”類來解決該問題,但最終會從數據庫中刪除所有項目并開始再次插入所有項目。這讓我思考性能。這是將新項目保存到數據庫的函數。“ContentDataCarrier”是包含有關項目的信息的類。(ID、名稱、效果、售價等) public void AddItemToDatabase(ContentDataCarrier contentDataCarrier) {            var databaseDocument = new XmlDocument();            // Load the database XML document with the file path.            // Note: We need to make sure if "AssetDatabase.GetAssetPath()" function also works at runtime.             databaseDocument.Load(AssetDatabase.GetAssetPath(_itemProgressDatabase));            // Now create new nodes for each property that "contentDataCarrier" has.            XmlNode itemWrapperNode = databaseDocument.CreateElement("item");            XmlNode idNode = databaseDocument.CreateElement("ID");            idNode.InnerText = LastAddedItemID++.ToString();            XmlNode levelNode = databaseDocument.CreateElement("level");            levelNode.InnerText = contentDataCarrier.Level.ToString();            XmlNode nameNode = databaseDocument.CreateElement("name");            nameNode.InnerText = contentDataCarrier.Name;             XmlNode descriptionNode = databaseDocument.CreateElement("description");            descriptionNode.InnerText = contentDataCarrier.Description;            XmlNode spriteNode = databaseDocument.CreateElement("sprite");            spriteNode.InnerText = "null"; // Somehow find a way to save sprite id and load it into game.}我希望在運行時將玩家收集的項目保存到 xml 文件中。
查看完整描述

1 回答

?
炎炎設計

TA貢獻1808條經驗 獲得超4個贊

使用 XMLSerializer,我可以隨時從“.xml”文件中保存和獲取數據。


using System.Collections.Generic;

using System.IO;

using System.Xml.Serialization;

using UnityEngine;


[DisallowMultipleComponent]

internal sealed class XMLManager : MonoBehaviour

{

? ? public ItemDatabase ItemDatabase = new ItemDatabase();


? ? public void SaveItem()

? ? {

? ? ? ? var xmlSerializer = new XmlSerializer(typeof(ItemDatabase));

? ? ? ? var fileStream = new FileStream(Application.dataPath + "/StreamingFiles/XML/Items.xml", FileMode.Create);


? ? ? ? xmlSerializer.Serialize(fileStream, ItemDatabase);


? ? ? ? fileStream.Close();

? ? }


? ? public void LoadItem()

? ? {

? ? ? ? var xmlSerializer = new XmlSerializer(typeof(ItemDatabase));

? ? ? ? var fileStream = new FileStream(Application.dataPath + "/StreamingFiles/XML/Items.xml", FileMode.Open);


? ? ? ? ItemDatabase = xmlSerializer.Deserialize(fileStream) as ItemDatabase;


? ? ? ? fileStream.Close();

? ? }

}


[System.Serializable]

public sealed class Item

{

? ? public string Name;

? ? public string Description;

? ? public int Damage;

? ? public Source Element;


? ? public enum Source { Fire, Water, Air };

}


[System.Serializable]

public sealed class ItemDatabase

{

? ? public List<Item> items = new List<Item>();

}

這是適合我的代碼。


希望這能幫助那里的人。


查看完整回答
反對 回復 2023-07-09
  • 1 回答
  • 0 關注
  • 176 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號