unity3d使用Mono.Xml读取xml
2014-03-27 23:05
211 查看
unity3d读取xml有好几种方式,最简单是直接利用System.Xml读取xml,但是项目打包会比较大,增加了1M的资源占用。另外两个是利用其他轻量级xml库来实现,如Mono.Xml、XMLParser。Mono.Xml是c#写的,XMLParser是js写的。文章主要说明Mono.Xml的用法。
为什么不建议使用System.Xml,unity的解释如下:
When building a player (Desktop, Android or iOS) it is important to not depend on System.dll or System.Xml.dll. Unity does not include System.dll or System.Xml.dll in the players installation. That means, if you want to use Xml or some Generic containers which live in System.dll then the required dlls will be included in the players. This usually adds 1mb to the download size, obviously this is not very good for the distribution of your players and you should really avoid it. If you need to parse some Xml files, you can use a smaller xml library like this one Mono.Xml.zip. While most Generic containers are contained in mscorlib, Stack<> and few others are in System.dll. So you really want to avoid those.
首先,定义一个xml文件,如下:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ROOT>
<table wave="1" level="1" name="John"/>
<table wave="2" level="1" name="Lucy"/>
</ROOT>把Mono.Xml加进unity3d项目。下载地址:http://download.csdn.net/detail/cwqcwk1/7105071
unity3d利用Mono.xml读取xml的代码如下:
using UnityEngine;
using System.Collections;
using Mono.Xml;
using System.IO;
using System.Security;
public class XmlLorder {
public void Read()
{
SecurityParser SP = new SecurityParser();
// 假设xml文件路径为 Resources/test.xml
string xmlPath = "test";
SP.LoadXml(Resources.Load( xmlPath ).ToString());
SecurityElement SE = SP.ToXml();
foreach (SecurityElement child in SE.Children)
{
//比对下是否使自己所需要得节点
if(child.Tag == "table")
{
//获得节点得属性
string wave = child.Attribute("wave");
string level = child.Attribute("level");
string name = child.Attribute("name");
Debug.Log("wave:" + wave + " level:" + level + " name:" + name);
}
}
}
}
顺带提一下unity3d使用XMLParser读取xml:
XMLParser xmlparse = new XMLParser();
XMLNode node = xmlparse.Parse(xmldata.text);
XMLNodeList list = node.GetNodeList("ROOT>0>table");
for (int i = 0; i < list.Count; i++)
{
string wave = node.GetValue("ROOT>0>table>" + i + ">@wave");
string level = node.GetValue("ROOT>0>table>" + i + ">@level");
string wait = node.GetValue("ROOT>0>table>" + i + ">@wait");
}XMLParser下载地址:http://download.csdn.net/detail/cwqcwk1/7104625
参考: http://blog.csdn.net/mycwq/article/details/19813779 http://docs.unity3d.com/Documentation/Manual/ReducingFilesize.html
为什么不建议使用System.Xml,unity的解释如下:
When building a player (Desktop, Android or iOS) it is important to not depend on System.dll or System.Xml.dll. Unity does not include System.dll or System.Xml.dll in the players installation. That means, if you want to use Xml or some Generic containers which live in System.dll then the required dlls will be included in the players. This usually adds 1mb to the download size, obviously this is not very good for the distribution of your players and you should really avoid it. If you need to parse some Xml files, you can use a smaller xml library like this one Mono.Xml.zip. While most Generic containers are contained in mscorlib, Stack<> and few others are in System.dll. So you really want to avoid those.
首先,定义一个xml文件,如下:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ROOT>
<table wave="1" level="1" name="John"/>
<table wave="2" level="1" name="Lucy"/>
</ROOT>把Mono.Xml加进unity3d项目。下载地址:http://download.csdn.net/detail/cwqcwk1/7105071
unity3d利用Mono.xml读取xml的代码如下:
using UnityEngine;
using System.Collections;
using Mono.Xml;
using System.IO;
using System.Security;
public class XmlLorder {
public void Read()
{
SecurityParser SP = new SecurityParser();
// 假设xml文件路径为 Resources/test.xml
string xmlPath = "test";
SP.LoadXml(Resources.Load( xmlPath ).ToString());
SecurityElement SE = SP.ToXml();
foreach (SecurityElement child in SE.Children)
{
//比对下是否使自己所需要得节点
if(child.Tag == "table")
{
//获得节点得属性
string wave = child.Attribute("wave");
string level = child.Attribute("level");
string name = child.Attribute("name");
Debug.Log("wave:" + wave + " level:" + level + " name:" + name);
}
}
}
}
顺带提一下unity3d使用XMLParser读取xml:
XMLParser xmlparse = new XMLParser();
XMLNode node = xmlparse.Parse(xmldata.text);
XMLNodeList list = node.GetNodeList("ROOT>0>table");
for (int i = 0; i < list.Count; i++)
{
string wave = node.GetValue("ROOT>0>table>" + i + ">@wave");
string level = node.GetValue("ROOT>0>table>" + i + ">@level");
string wait = node.GetValue("ROOT>0>table>" + i + ">@wait");
}XMLParser下载地址:http://download.csdn.net/detail/cwqcwk1/7104625
参考: http://blog.csdn.net/mycwq/article/details/19813779 http://docs.unity3d.com/Documentation/Manual/ReducingFilesize.html
相关文章推荐
- unity3d使用Mono.Xml读取xml
- unity3d使用Mono.Xml读取xml
- 在Unity3D中使用Mono.XML读取XML文件
- 使用Unity3d官方提供得精简版xml解析脚本Mono.xml
- Unity3D 使用link.xml来控制strippinglevel减少dll库大小技巧、优化安装包大小
- Unity3D 使用 C#合成解析XML与JSON
- Dom4j使用Xpath语法读取xml节点
- 使用xml.dom.minidom读取xml文件中的值
- 使用Qt中的QXmlStreamReader读取XML文件
- JAVA操作XML一(读取):使用DOM读取XML数据的两种具体实现
- 使用XmlDocument 读取books.xml中的书名到DropDownList中
- 在Windows下使用TinyXML-2读取UTF-8编码包含中文字符的XML文件
- 使用XMLReader、XMLDocument和DataSet读取xml文件及效率
- 使用javascript中读取Xml文件做成的一个二级联动菜单
- Silverlight实用窍门系列:1.Silverlight读取外部XML加载配置---(使用WebClient读取XAP包同目录下的XML文件))【附带实例源码】
- Unity3D研究院之使用 C#合成解析XML与JSON(四十一)(转自雨松MOMO)
- 使用XMLList读取XML数据
- [Qt教程] 第27篇 XML(一)使用DOM读取XML文档
- JAVA:使用Java读取XML数据
- Java:XML篇,使用JDOM读取并解析XML