在Unity3D中使用Mono.XML读取XML文件

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

转自: http://www.lxway.com/4418259524.htm


1、XML文件如下:

  1. <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  2. <Root xmlns:xsiRootxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  3. <Row>
  4. <Level>1</Level>
  5. <Coin>2323</Coin>
  6. <Exp>432</Exp>
  7. </Row>
  8. <Row>
  9. <Level>2</Level>
  10. <Coin>2342</Coin>
  11. <Exp>4444</Exp>
  12. </Row>
  13. </Root>



把这个XML文件(假定文件名为data.xml)放到Unity3D项目的任意Resources目录下,使用过如下脚本来访问(以查询Level为2的数据项为例):


  1. using System.IO;
  2. using Mono.Xml;
  3.  
  4. void ReadXML(){
  5. SecurityParser SP = new SecurityParser();
  6. SP.LoadXml(Resources.Load("data").ToString());
  7. System.Security.SecurityElement SE = SP.ToXml();
  8.  
  9. foreach (System.Security.SecurityElement child in SE.Children)
  10. {
  11. if (int.Parse(child.SearchForChildByTag("Level").Text) == 2)
  12. {
  13. coin = int.Parse(child.SearchForChildByTag("Coin").Text);
  14. exp = int.Parse(child.SearchForChildByTag("Exp").Text);
  15. Debug.Log("level:2\t" +"\texpNum:" + exp +"\tcoinNum:" + coin);
  16. break;
  17. }
  18. }
  19. }

添加一句,话说使用Mono的XML可以减少对System库的引用,在输出到移动设备时可以减少内存的占用哦。其实这才是这个东东的最大意义。

PS:Mono.xml可以在 http://download.csdn.net/detail/reipeng/5107900 下载。直接放到Unity3d项目中就可用了。

猜你在找的XML相关文章