在游戏开发中。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结果:

Unity3d 新建xml 读取xml的更多相关文章

  1. Linq to XML 读取XML 备忘笔记

    本文转载:http://www.cnblogs.com/infozero/archive/2010/07/13/1776383.html Linq to XML 读取XML 备忘笔记 最近一个项目中有 ...

  2. C#基础巩固(3)-Linq To XML 读取XML

    记录下一些读取XML的方法,以免到用的时候忘记了,还得花时间去找. 一.传统写法读取XML 现在我有一个XML文件如下: 现在我要查找名字为"王五"的这个人的 Id 和sex(性别 ...

  3. C#使用Linq To XML读取XML,Linq生成XML,Linq创建带属性或带节点XML

    using System; using System.Linq; using System.Xml.Linq; namespace Sample2 { class Program { static v ...

  4. 在相应目录下新建或读取xml文件

    string path = AppDomain.CurrentDomain.BaseDirectory+"UserContent1.xml"; //判断相应路径下文件是否存在 不存 ...

  5. Linq to xml 读取xml文件或xml字符串

    XMLFile1.xml: XDocument Contacts = XDocument.Load("XMLFile1.xml"); //XElement Contacts = X ...

  6. C#基础笔记---浅谈XML读取以及简单的ORM实现

    背景: 在开发ASP.NETMVC4 项目中,虽然web.config配置满足了大部分需求,不过对于某些特定业务,我们有时候需要添加新的配置文件来记录配置信息,那么XML文件配置无疑是我们选择的一个方 ...

  7. C#基础---浅谈XML读取以及简单的ORM实现

    背景: 在开发ASP.NETMVC4 项目中,虽然web.config配置满足了大部分需求,不过对于某些特定业务,我们有时候需要添加新的配置文件来记录配置信息,那么XML文件配置无疑是我们选择的一个方 ...

  8. PHP读取xml方法讲解

    一,什么是xml,xml有什么用途 XML(Extensible Markup Language)即可扩展标记语言,它与HTML一样,都是SGML(Standard Generalized Marku ...

  9. 读取xml到DataSet中去

    XML如下: <?xml version="1.0" encoding="utf-8" ?> <Config> <System&g ...

随机推荐

  1. 你所在的公司是如何实施DEVOPS的?

    工欲善其事,必先利其器,现在大家在DevOps领域最关注的还是在工具层面.下面是我跟这么多公司接触下来,大家使用比较多的工具:1.监控工具比较老牌的就是Zabbix,Nagios,用Zabbix的感觉 ...

  2. 推荐个PMP的内容,广州有需要的朋友可以参考看看

    慧翔天地PMP®培训机构简介 广州慧翔企业管理咨询有限公司注册于2012年8月14日,注册资金200万元人民币.实际上从2011年就已经开始从事PMP推广及教学工作(2010年曾代理智鼎东方华南市场, ...

  3. BNU 12846 LCM Extreme 最小公倍数之和(线性欧拉筛选+递推)

    LCM Extreme Time Limit: 3000ms Memory Limit: 131072KB   This problem will be judged on UVALive. Orig ...

  4. LeetCode OJ-- Sqrt(x) *

    https://oj.leetcode.com/problems/sqrtx/ 求一个数的开方,二分查找一个数,直到这个数的平方 - target 小于 0.001. 但这道题出的不好,返回值竟然是 ...

  5. Codeforces 597B Restaurant(离散化 + 贪心)

    题目链接 Restaurant 题目意思就是在$n$个区间内选出尽可能多的区间,使得这些区间互不相交. 我们先对这$n$个区间去重. 假如有两个区间$[l1, r1],[l2, r2]$ 若满足$l1 ...

  6. 洛谷——P2737 [USACO4.1]麦香牛块Beef McNuggets

    https://www.luogu.org/problemnew/show/P2737 题目描述 农夫布朗的奶牛们正在进行斗争,因为它们听说麦当劳正在考虑引进一种新产品:麦香牛块.奶牛们正在想尽一切办 ...

  7. ios 6.0模拟器页面调出pop窗口消失后无法使用键盘

    ios 6模拟器上,点击事件调用出pop窗口,这个窗口新创建了window,在pop窗口消失的函数中使用了makeKeyWindow,这个是将要显示的window放到最前端.发现 屏蔽这个方法后可以了 ...

  8. OpenGL step to step(1)

    在窗体上绘制一个矩形,just a demo #include <GLUT/GLUT.h> void init() { glClearColor(0.0,0.0,0.0,0.0); glS ...

  9. 记Weblogic采用RAC方式链接数据库遇到的问题

      前几天,去客户现场部署系统,WEBLOGIC连接数据库使用RAC方式连接,好几个人弄了一下午愣是没搞定,总是报SID错误 开始一致认为是防火墙的原因,后来SSH登陆应用服务器后,再TELNET数据 ...

  10. 七天学会ASP.NET MVC (一)——深入理解ASP.NET MVC 【转】

    http://www.cnblogs.com/powertoolsteam/p/MVC_one.html 系列文章 七天学会ASP.NET MVC (一)——深入理解ASP.NET MVC 七天学会A ...