本文共 5144 字,大约阅读时间需要 17 分钟。
以下是一个示例的原始XML数据结构,用于存储玩家信息:
shanJia 10 5 20 20 1 1 123 123
以下是读取XML数据的示例代码:
using System.Collections;using System.Collections.Generic;using System.IO;using System.Xml;using UnityEngine;public class Item{ public int id; public int num;}public class PlayerInfo : MonoBehaviour{ public string name; public int atk; public int def; public float moveSpeed; public float roundSpeed; public Item weapon = new Item(); public List listInt = new List (); public List- itemList = new List
- (); public Dictionary
itemDic = new Dictionary (); public void LoadData(string fileName) { string path = Application.persistentDataPath + "/" + fileName + ".xml"; if (!File.Exists(path)) { path = Application.streamingAssetsPath + "/" + fileName + ".xml"; } XmlDocument xml = new XmlDocument(); xml.Load(path); XmlNode root = xml.SelectSingleNode("PlayerInfo"); name = root.SelectSingleNode("name").InnerText; atk = int.Parse(root.SelectSingleNode("atk").InnerText); def = int.Parse(root.SelectSingleNode("def").InnerText); moveSpeed = int.Parse(root.SelectSingleNode("moveSpeed").InnerText); roundSpeed = int.Parse(root.SelectSingleNode("roundSpeed").InnerText); weapon.id = int.Parse(root.SelectSingleNode("weapon").SelectSingleNode("id").InnerText); weapon.num = int.Parse(root.SelectSingleNode("weapon").SelectSingleNode("num").InnerText); XmlNode intList = root.SelectSingleNode("listInt"); XmlNodeList lsit = intList.SelectNodes("int"); for (int i = 0; i < lsit.Count; i++) { listInt.Add(int.Parse(lsit[i].InnerText)); } XmlNodeList Itemlist = root.SelectSingleNode("itemList").SelectNodes("Item"); for (int i = 0; i < Itemlist.Count; i++) { Item t = new Item(); t.id = int.Parse(Itemlist[i].Attributes["id"].Value); t.num = int.Parse(Itemlist[i].Attributes["num"].Value); itemList.Add(t); } XmlNodeList dic = root.SelectSingleNode("itemDic").SelectNodes("Item"); foreach (XmlNode item in dic) { Item item1 = new Item(); item1.id = int.Parse(item.Attributes["id"].Value); item1.num = int.Parse(item.Attributes["num"].Value); itemDic.Add(item1.id, item1); } }}
以下是存储数据的示例代码:
public void SaveData(string fileName){ string path = Application.persistentDataPath + "/" + fileName + ".xml"; XmlDocument xml = new XmlDocument(); XmlDeclaration xmlDec = xml.CreateXmlDeclaration("1.0", "UTF-8", ""); xml.AppendChild(xmlDec); XmlElement root = xml.CreateElement("PlayerInfo"); xml.AppendChild(root); XmlElement name = xml.CreateElement("name"); name.InnerText = name; root.AppendChild(name); XmlElement atk = xml.CreateElement("atk"); atk.InnerText = atk.ToString(); root.AppendChild(atk); XmlElement def = xml.CreateElement("def"); def.InnerText = def.ToString(); root.AppendChild(def); XmlElement moveSpeed = xml.CreateElement("moveSpeed"); moveSpeed.InnerText = moveSpeed.ToString(); root.AppendChild(moveSpeed); XmlElement roundSpeed = xml.CreateElement("roundSpeed"); roundSpeed.InnerText = roundSpeed.ToString(); root.AppendChild(roundSpeed); XmlElement weapon = xml.CreateElement("weapon"); XmlElement id = xml.CreateElement("id"); id.InnerText = weapon.id.ToString(); XmlElement num = xml.CreateElement("num"); num.InnerText = weapon.num.ToString(); weapon.AppendChild(id); weapon.AppendChild(num); root.AppendChild(weapon); XmlElement listInt = xml.CreateElement("listInt"); for (int i = 0; i < listInt.Count; i++) { XmlElement intNode = xml.CreateElement("int"); intNode.InnerText = listInt[i].ToString(); listInt.AppendChild(intNode); } root.AppendChild(listInt); XmlElement itemList = xml.CreateElement("itemList"); for (int i = 0; i < itemList.Count; i++) { XmlElement itemNode = xml.CreateElement("Item"); itemNode.SetAttribute("id", itemList[i].id.ToString()); itemNode.SetAttribute("num", itemList[i].num.ToString()); itemList.AppendChild(itemNode); } root.AppendChild(itemList); XmlElement itemDic = xml.CreateElement("itemDic"); foreach (int key in itemDic.Keys) { XmlElement intNode = xml.CreateElement("int"); intNode.InnerText = key.ToString(); itemDic.AppendChild(intNode); XmlElement itemNode = xml.CreateElement("Item"); itemNode.SetAttribute("id", itemDic[key].id.ToString()); itemNode.SetAttribute("num", itemDic[key].num.ToString()); itemDic.AppendChild(itemNode); } root.AppendChild(itemDic); xml.Save(path);}
通过以上代码示例,可以实现对XML数据的读取和存储。读取部分主要是从XML文件中提取数据并赋值给相应的变量,存储部分则是将游戏中的数据以XML格式写入文件中。这种方式可以方便地进行数据的交互和管理。
转载地址:http://fvwzz.baihongyu.com/