在游戏开发中。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. spring和resteasy 的集成方式

    spring和resteasy集成,三种主要的方式, 对于和jboss as7的集成不需要做任何工作,jboss默认集成了resteasy,只需要对业务pojo做一些jax-rs的注解标注即可.这里讲 ...

  2. UVa1362 Exploring Pyramids

    区间dp,枚举走完第一个子树之后回到根节点的位置. /*by SilverN*/ #include<algorithm> #include<iostream> #include ...

  3. LeetCode OJ--Binary Tree Level Order Traversal II

    http://oj.leetcode.com/problems/binary-tree-level-order-traversal-ii/ 树的层序遍历,和上一道题相比,对结果做一个顺序调整 reve ...

  4. git 中遇到的问题

    刚创建的github版本库,在push代码时出错: $ git push -u origin masterTo git@github.com:******/Demo.git ! [rejected] ...

  5. 机器学习实战读书笔记(二)k-近邻算法

    knn算法: 1.优点:精度高.对异常值不敏感.无数据输入假定 2.缺点:计算复杂度高.空间复杂度高. 3.适用数据范围:数值型和标称型. 一般流程: 1.收集数据 2.准备数据 3.分析数据 4.训 ...

  6. SSH命令总结

    目录 一.ssh命令 二.端口转发 三.scp 命令 四.rsync命令 五.sz和rz命令 六. ssh-agent 七.ssh执行命令不退出问题 参考文章 一.ssh命令 登录类型 密码登录: 服 ...

  7. 快速上手 Echarts

    最近使用到了 百度的 Echarts 数据可视化工具,这里简单介绍如何快速上手. 一.下载 这里选择目前最新版本,4.2.1 地址:https://github.com/apache/incubato ...

  8. spring的自动装配Bean与自动检测Bean

    spring可以通过编写XML来配置Bean,也可以通过使用spring的注解来装配Bean. 1.自动装配与自动检测: 自动装配:让spring自动识别如何装配bean的依赖关系,减少对<pr ...

  9. 深刻理解JavaScript---闭包

    JavaScript   闭包是指那些能够访问独立(自由)变量的函数 (变量在本地使用,但定义在一个封闭的作用域中).换句话说,这些函数可以“记忆”它被创建时候的环境.——这句话其实有点难以理解.我觉 ...

  10. Android 震动 和 停止 代码

    <span style="font-size:18px;">MainActivity.java</span> package com.Utils.vibra ...