在游戏开发中。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. 关于cookie使用的一些问题

    保存cookie后提取出来发现字符串是被编码过的,需要decodeURIComponent进行下解码才可以 设置cookie setCookie(c_name, value, expiredays) ...

  2. TroubleShoot:The context has expired (0×80090317)

    网上搜了一下,服务器上的时间不正确,在SharePoint 设置中,可以通过管理中心设置下Time Zone 和服务器的时间上一致.

  3. Codevs 数字三角形 问题合集

    1220 数字三角形 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 如图所示的数字三角形,从顶部出发,在每一结点可以选择向左走或得 ...

  4. 禁用VMware的vmem文件

    新建一个虚拟机,VMWare会默认为其创建一个虚拟内存文件*.VMEM, 这个文件会影响系统的磁盘性能,所以最好关闭它. 该当是找到*.vmx文件,在文件最后加入一行 mainMem.useNamed ...

  5. android 笔记(Service)

    Service 一.Serivce的启动方式分两种 1.startService.用这种方式启动的话,负责启动这个service的Activity或者其他组件即使被销毁了,Service也会继续在后台 ...

  6. Xamarin.Forms中为WebView指定数据来源Source

    Xamarin.Forms中为WebView指定数据来源Source   网页视图WebView用来显示HTML和网页形式内容.使用这种方式,可以借助网页形式进行界面设计,并利于更新和维护.WebVi ...

  7. codeforces 979E(dp套dp)

    题意: 有n个点,编号为1~n.有的点颜色是黑色,有的点颜色是白色,有的点的颜色待涂.你还可以连一些边,但这些边一定是从小编号连到大编号的点. 对于一个确定的图,我们去统计有多少条路径满足“该路径经过 ...

  8. Java 浅析,生成OFD文件

    摘要:这几天遇到个需要,需要提供用户下载电子证照,最简单的方法实现:word做了一份模板,利用网页工具转成OFD文件,http://www.yozodcs.com/page/example.html用 ...

  9. Hystrix的介绍和简单使用

    这周在看项目的相关代码时,接触到了Hystrix,因此查询了相关资料学习了下. 一.什么是Hystrix Hystrix是Netflix针对微服务分布式系统的熔断保护中间件,当我们的客户端连接远程的微 ...

  10. Codeforces Round #295 (Div. 1) C. Pluses everywhere

    昨天ZZD大神邀请我做一道题,说这题很有趣啊. 哇,然后我被虐了. Orz ZZD 题目大意: 你有一个长度为n的'0-9'串,你要在其中加入k个'+'号,每种方案就会形成一个算式,算式算出来的值记做 ...