Unity3d 新建xml 读取xml
XML 读取 Unity3D 新建
2023-09-11 14:14:09 时间
在游戏开发中,Xml常常被用来作为技能配置、地图配置、人物动作配置等配置文件。Unity3d内置的Xml库让我们非常方便地就能够新建Xml和读取Xml。
以下是一个样例,新建了一个Xml文档,而且读取它。
using UnityEngine; using System.Collections; using System.IO; using System.Xml; using System.Text; public class XmlTest : MonoBehaviour { XmlElement m_roleMotions = null;//人物动作; XmlElement m_skills = null;//人物技能; // Use this for initialization void Start () { //CreateXml(); //ReadXml(); ReadFileToXml(); } // Update is called once per frame void Update () { } void CreateXml() { string filepath = Application.dataPath + "/Resources/1013000.xml"; if (!File.Exists(filepath)) { //创建xml实例; XmlDocument xmlDoc = new XmlDocument(); //创建character; XmlElement root = xmlDoc.CreateElement("character"); /***创建roleMotions Start***/ XmlElement roleMotions = xmlDoc.CreateElement("roleMotions"); XmlElement motionInfo = xmlDoc.CreateElement("motionInfo"); XmlElement motion = xmlDoc.CreateElement("motion"); motion.SetAttribute("clipName", "enter_ready"); motion.SetAttribute("isLoop", "false"); motion.SetAttribute("moveEndTime", "0"); motion.SetAttribute("moveStartTime", "0"); motionInfo.AppendChild(motion); roleMotions.AppendChild(motionInfo); root.AppendChild(roleMotions); /***创建roleMotions End***/ /***创建skills Start***/ XmlElement skills = xmlDoc.CreateElement("skills"); XmlElement skill = xmlDoc.CreateElement("skill"); skill.SetAttribute("name", "普攻"); skill.SetAttribute("motion", "RMT_Attack1"); skills.AppendChild(skill); root.AppendChild(skills); /***创建skills End***/ xmlDoc.AppendChild(root); xmlDoc.Save(filepath); } else { Debug.LogError("File hava exist"); } } void ReadXml() { string filepath = Application.dataPath + "/Resources/1013000.xml"; if (!File.Exists(filepath)) { Debug.LogError("xml file not exist"); return; } XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(filepath); //获取全部子节点; XmlNodeList nodeList = xmlDoc.SelectSingleNode("character").ChildNodes; foreach(XmlNode child in nodeList) { if (child.Name == "roleMotions") { m_roleMotions = child as XmlElement; } else if (child.Name == "skills") { m_skills = child as XmlElement; } } Debug.Log("m_roleMotions = " + m_roleMotions.InnerXml); Debug.Log("m_skills = " + m_skills.InnerXml); } void ReadFileToXml() { string filepath = "1013000"; GameObject obj = Resources.Load(filepath) as GameObject; TextAsset xmlAsset = Resources.Load(filepath,typeof(TextAsset)) as TextAsset; XmlDocument xmlDoc = new XmlDocument(); xmlDoc.LoadXml(xmlAsset.text); //获取全部子节点; XmlNodeList nodeList = xmlDoc.SelectSingleNode("character").ChildNodes; foreach (XmlNode child in nodeList) { if (child.Name == "roleMotions") { m_roleMotions = child as XmlElement; } else if (child.Name == "skills") { m_skills = child as XmlElement; } } Debug.Log("m_roleMotions = " + m_roleMotions.InnerXml); Debug.Log("m_skills = " + m_skills.InnerXml); } }
新建的Xml文档内容例如以下:
<character> <roleMotions> <motionInfo> <motion clipName="enter_ready" isLoop="false" moveEndTime="0" moveStartTime="0" /> </motionInfo> </roleMotions> <skills> <skill name="普攻" motion="RMT_Attack1" /> </skills> </character>
读取Xml结果:
相关文章
- XML和JSON数据格式
- xml介绍+使用java_xml创建+xml读取
- XML总结
- Java&Xml教程(十一)JAXB实现XML与Java对象转换
- python读取xml文件报错ValueError: multi-byte encodings are not supported
- ASP.NET MVC 学习笔记-2.Razor语法 ASP.NET MVC 学习笔记-1.ASP.NET MVC 基础 反射的具体应用 策略模式的具体应用 责任链模式的具体应用 ServiceStack.Redis订阅发布服务的调用 C#读取XML文件的基类实现
- 浅谈JS中的!=、== 、!==、===的用法和区别 JS中Null与Undefined的区别 读取XML文件 获取路径的方式 C#中Cookie,Session,Application的用法与区别? c#反射 抽象工厂
- Word控件Spire.Doc 转换教程(一):在 C#、VB.NET 中将 Word Doc 转换为 XML
- Delphi保存xml格式文件无法读取原因
- 使用Pull解析器生成XML文件和读取xml文件
- SharedPreference对象及其xml文件
- c#读取string类型的xml格式的字符串
- Axis2 Web服务配置文件services.xml详解
- 信息: Loading XML bean definitions from class path resource [bean.xml]
- java.lang.ClassCastException:weblogic.xml.jaxp.RegistryDocumentBuilderFactory cannot be cast to javax.xml.parsers.DocumentBuilderFactory
- Dom4j解析Xml文件,Dom4j创建Xml文件
- 【XML和Java】手写Java程序引用xsd验证xml
- JAXB xml序列化应注意
- C#中对xml数据的读取和写入
- java解析XML学习总结——SAXReader解析xml文件数据
- 一个XML解析实例
- PostgreSQL处理xml数据初步
- JSON与XML的区别比较(转)
- SSH深度历险(十一) AOP原理及相关概念学习+xml配置实例(对照注解方式的优缺点)
- 基于docker创建的Jenkins,settings.xml文件放在哪里
- Mybatis自动生成Xml文件,针对字段类型为text等会默认产生XXXXWithBlobs的方法问题
- C# 创建XML文档
- C#之读取web上的xml