Unity3D Mono.Xml和XmlParser的使用

前端之家收集整理的这篇文章主要介绍了Unity3D Mono.Xml和XmlParser的使用前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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文件,如下:

[html] view plain copy
  1. <?xmlversion="1.0"encoding="UTF-8"standalone="yes"?>
  2. <ROOT>
  3. tablewave="1"level="1"name="John"/>
  4. tablewave="2"level="1"name="Lucy"/>
  5. </>

把Mono.Xml加进unity3d项目。下载地址:http://download.csdn.net/detail/mylefteyeisl/7973567

unity3d利用Mono.xml读取xml的代码如下:

[csharp]
    usingUnityEngine;
  1. usingSystem.Collections;
  2. usingMono.Xml;
  3. usingSystem.IO;
  4. usingSystem.Security;
  5. publicclassXmlLorder{
  6. voidRead()
  7. {
  8. SecurityParserSP=newSecurityParser();
  9. //假设xml文件路径为Resources/test.xml
  10. stringxmlPath="test.xml";
  11. SP.LoadXml(Resources.Load(xmlPath).ToString());
  12. SecurityElementSE=SP.ToXml();
  13. foreach(SecurityElementchildinSE.Children)
  14. {
  15. //比对下是否使自己所需要得节点
  16. if(child.Tag=="table")
  17. //获得节点得属性
  18. stringwave=child.Attribute("wave");
  19. stringlevel=child.Attribute("level");
  20. stringname=child.Attribute("name");
  21. Debug.Log("wave:"+wave+"level:"+level+"name:"+name);
  22. }
  23. }
  24. }

顺带提一下unity3d使用XMLParser读取xml:

[javascript]
    XMLParserxmlparse=newXMLParser();
  1. XMLNodenode=xmlparse.Parse(xmldata.text);
  2. XMLNodeListlist=node.GetNodeList("ROOT>0>table");
  3. for(inti=0;i<list.Count;i++)
  4. stringwave=node.GetValue("ROOT>0>table>"+i+">@wave");
  5. stringlevel=node.GetValue("ROOT>0>table>"+i+">@level");
  6. stringwait=node.GetValue("ROOT>0>table>"+i+">@wait");
  7. }

XMLParser下载地址:http://download.csdn.net/detail/mylefteyeisl/7973567

猜你在找的XML相关文章