在游戏开发中。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. LeetCode OJ——Word Ladder

    http://oj.leetcode.com/problems/word-ladder/ 图的最短路径问题,可以用最短路径算法,也可以深搜,也可以广搜. 深搜版本: 第一次写的时候,把sum和visi ...

  2. AC日记——方格取数 洛谷 P1004

    题目描述 设有N*N的方格图(N<=9),我们将其中的某些方格中填入正整数,而其他的方格中则放 人数字0.如下图所示(见样例): A 0 0 0 0 0 0 0 0 0 0 13 0 0 6 0 ...

  3. Python递归函数和二分查找算法

    递归函数:在一个函数里在调用这个函数本身. 递归的最大深度:998 正如你们刚刚看到的,递归函数如果不受到外力的阻止会一直执行下去.但是我们之前已经说过关于函数调用的问题,每一次函数调用都会产生一个属 ...

  4. F#周报2019年第21期

    新闻 F#在GitHub上的开发仓库现在变为dotnet/fsharp Ionide 4.0路线图 Fable的五月公告 Visual Studio 2019版本16.1 WinUI 3.0路线图 欢 ...

  5. 思科CCIE全新升级,SDN/SD-WAN成重头戏!

    CCIE,全称Cisco Certified Internetwork Expert,是美国Cisco公司于1993年开始推出的专家级认证考试.被全球公认为IT业最权威的认证,是全球Internetw ...

  6. 弹出层layer演示 以及在编写弹出层时遇到的错误

    实现的功能: 首先第一步 在官方下载layer的文件.layUI官网:http://layer.layui.com/     http://layer.layui.com/ layer文件的下载步骤如 ...

  7. commons-lang3工具类学习--------ObjectUtils

    Object工具类 allNotNull(Object... values) 检查所有元素是否为空,返回一个boolean 如果有一个元素为空返回false,所有元素不为空或元素为empty返回tru ...

  8. Java判断语句中判断条件的执行顺序

    if判断里面如果是多个条件的判断,会按照从前往后的顺序执行.基本所有语言都是一致的设计,这样做能提高程序的效率. 比如: if ( conditionA && conditionB ) ...

  9. Linux下快速删除输错的密码技巧(快速删除输入的命令)

    1.[Esc]+[退格键(Backspace)] 2.[Ctrl]+[U] 说明:以上两个快捷键都会删除全部输错的命令或密码. 参考: http://blog.csdn.net/u013895662/ ...

  10. 常见编码bug

    1.result.replace("abc","bcd");错误 改成result= result.relace("abc","b ...