源码分享 收藏本版 已有3人收藏 +发表新主题
查看: 18062|回复: 0
打印 上一主题 下一主题

[分享] Unity操作XML例子,比官方WIKI的简单

[复制链接]

[分享] Unity操作XML例子,比官方WIKI的简单

yanjianghao 发表于 2016-5-5 10:19:51 浏览:  18062 回复:  0 只看该作者 复制链接
在百度找了3页unity使用XML的相关资料,清一色全是官方的例子,比较繁琐。。于是就使用C#调用XML的方法尝试了一下。

这是第一个例程,测试创建一个简单的XML文件。使用时先调用xmlInit()方法初始化,然后调用writeTest()写入。

可能是我的unity版本问题,程序在build之后xml读写就没用了。
一直没找到解决方法。于是自己写了一个XML类(不支持序列化):http://game.ceeger.com/forum/read.php?tid=4989

如果下面的例程序没用的话,可以换用我写的那个。
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.IO;
  4. using System.Xml;

  5. public class XmlTest : MonoBehaviour
  6. {
  7.         private string fileName = @"/config.xml";

  8.         private string path = "";

  9.         private XmlDocument xd = new XmlDocument();
  10.         private XmlElement rootEle;

  11.         public void xmlInit()
  12.         {
  13.             path = System.IO.Directory.GetCurrentDirectory() + fileName;

  14.             rootEle = xd.CreateElement("root");
  15.             XmlDeclaration xdc = xd.CreateXmlDeclaration("1.0", "GBK", null);//这里是设置XML的版本号,不是自己游戏的版本号

  16.             xd.AppendChild(xdc);
  17.         }


  18.         public void writeTest()
  19.         {
  20.             XmlElement ele = xd.CreateElement("firstXml");

  21.             ele.InnerText = "hi";
  22.             rootEle.AppendChild(ele);
  23.             xd.AppendChild(rootEle);
  24.             xd.Save(path);
  25.         }
  26. }
复制代码

第二个例程。一个具有读写功能的XML类。用于设置游戏玩家的属性配置文件。并把XmlElement变为结构体。调用更方便。
同样,测试使用时先调用xmlInit(),再调用writeTest()或readTest().
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.IO;
  4. using System.Xml;

  5. public class XmlTest : MonoBehaviour
  6. {
  7.     private string fileName = @"/config.xml";
  8.    
  9.     private string path = "";
  10.    
  11.     private XmlDocument xd = new XmlDocument();
  12.     private XmlElement rootEle;

  13.    
  14.     public void xmlInit()
  15.     {
  16.         path = Directory.GetCurrentDirectory() + fileName;
  17.    
  18.         rootEle = xd.CreateElement("root");
  19.         XmlDeclaration xdc = xd.CreateXmlDeclaration("1.0", "GBK", null);//这里是设置XML的版本号,不是自己游戏的版本号
  20.    
  21.         xd.AppendChild(xdc);
  22.     }
  23.    
  24.    
  25.     public void writeTest()
  26.     {
  27.         string[] players = new string[]{"player1", "player2", "player3"};
  28.         string[] hps = new string[]{"100","200","150"};
  29.         string[] mps = new string[]{"110","62","94"};
  30.         for(int i=0;i <players.Length; i++)
  31.         {
  32.             XmlElement info = xd.CreateElement("info");
  33.             info.SetAttribute("name",players[i]);
  34.             Sct_XmlEles xes = new Sct_XmlEles();
  35.             
  36.             xes.hp = xd.CreateElement("hp");
  37.             xes.hp.InnerText = hps[i];
  38.             xes.mp = xd.CreateElement("mp");
  39.             xes.mp.InnerText = mps[i];
  40.             
  41.             info.AppendChild(xes.hp);
  42.             info.AppendChild(xes.mp);
  43.             rootEle.AppendChild(info);
  44.         }
  45.         xd.AppendChild(rootEle);
  46.         xd.Save(path);
  47.     }
  48.    
  49.     public void readTest()
  50.     {
  51.         string tmpStr = "";
  52.         xd.Load(path);
  53.         
  54.         foreach (XmlNode node1 in xd.ChildNodes)
  55.         {
  56.             foreach (XmlNode node2 in node1.ChildNodes)
  57.             {
  58.                 foreach (XmlNode node3 in node2.ChildNodes)
  59.                 {
  60.                     print (node2.Attributes[0].Name+"  -----  "+node2.Attributes[0].Value);
  61.                     switch(node3.Name)
  62.                     {
  63.                         case "hp":
  64.                             print ("hp   "+node3.InnerText);
  65.                         break;
  66.                         
  67.                         case "mp":
  68.                             print ("mp   "+node3.InnerText);
  69.                         break;
  70.                     }
  71.                 }
  72.             }
  73.         }
  74.     }
  75. }

  76. public struct Sct_XmlEles
  77. {
  78.     public XmlElement hp;
  79.     public XmlElement mp;
  80. }
复制代码
分享至:
| 人收藏
回复

使用道具 举报

*滑动验证:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Copyright © 2013-2017 ARinChina-增强现实中国技术论坛   All Rights Reserved.