转自: http://www.lxway.com/4418259524.htm
1、XML文件如下:
- <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
- <Root xmlns:xsiRootxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
- <Row>
- <Level>1</Level>
- <Coin>2323</Coin>
- <Exp>432</Exp>
- </Row>
- <Row>
- <Level>2</Level>
- <Coin>2342</Coin>
- <Exp>4444</Exp>
- </Row>
- </Root>
把这个XML文件(假定文件名为data.xml)放到Unity3D项目的任意Resources目录下,使用过如下脚本来访问(以查询Level为2的数据项为例):
- using System.IO;
- using Mono.Xml;
- void ReadXML(){
- SecurityParser SP = new SecurityParser();
- SP.LoadXml(Resources.Load("data").ToString());
- System.Security.SecurityElement SE = SP.ToXml();
- foreach (System.Security.SecurityElement child in SE.Children)
- {
- if (int.Parse(child.SearchForChildByTag("Level").Text) == 2)
- {
- coin = int.Parse(child.SearchForChildByTag("Coin").Text);
- exp = int.Parse(child.SearchForChildByTag("Exp").Text);
- Debug.Log("level:2\t" +"\texpNum:" + exp +"\tcoinNum:" + coin);
- break;
- }
- }
- }
添加一句,话说使用Mono的XML可以减少对System库的引用,在输出到移动设备时可以减少内存的占用哦。其实这才是这个东东的最大意义。
PS:Mono.xml可以在 http://download.csdn.net/detail/reipeng/5107900 下载。直接放到Unity3d项目中就可用了。