在游戏开发中。Xml常常被用来作为技能配置、地图配置、人物动作配置等配置文件。

Unity3d内置的Xml库让我们非常方便地就能够新建Xml和读取Xml。

以下是一个样例,新建了一个Xml文档。而且读取它。

using UnityEngine;
using System.Collections;
using System.IO;
using System.Xml;
using System.Text; public class XmlTest : MonoBehaviour { XmlElement m_roleMotions = null;//人物动作;
XmlElement m_skills = null;//人物技能; // Use this for initialization
void Start () {
//CreateXml();
//ReadXml();
ReadFileToXml();
} // Update is called once per frame
void Update () { } void CreateXml()
{
string filepath = Application.dataPath + "/Resources/1013000.xml";
if (!File.Exists(filepath))
{
//创建xml实例;
XmlDocument xmlDoc = new XmlDocument(); //创建character;
XmlElement root = xmlDoc.CreateElement("character"); /***创建roleMotions Start***/
XmlElement roleMotions = xmlDoc.CreateElement("roleMotions");
XmlElement motionInfo = xmlDoc.CreateElement("motionInfo");
XmlElement motion = xmlDoc.CreateElement("motion");
motion.SetAttribute("clipName", "enter_ready");
motion.SetAttribute("isLoop", "false");
motion.SetAttribute("moveEndTime", "0");
motion.SetAttribute("moveStartTime", "0");
motionInfo.AppendChild(motion);
roleMotions.AppendChild(motionInfo);
root.AppendChild(roleMotions);
/***创建roleMotions End***/ /***创建skills Start***/
XmlElement skills = xmlDoc.CreateElement("skills");
XmlElement skill = xmlDoc.CreateElement("skill");
skill.SetAttribute("name", "普攻");
skill.SetAttribute("motion", "RMT_Attack1");
skills.AppendChild(skill);
root.AppendChild(skills);
/***创建skills End***/ xmlDoc.AppendChild(root); xmlDoc.Save(filepath);
}
else
{
Debug.LogError("File hava exist");
}
} void ReadXml()
{
string filepath = Application.dataPath + "/Resources/1013000.xml";
if (!File.Exists(filepath))
{
Debug.LogError("xml file not exist");
return;
}
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(filepath); //获取全部子节点;
XmlNodeList nodeList = xmlDoc.SelectSingleNode("character").ChildNodes;
foreach(XmlNode child in nodeList)
{
if (child.Name == "roleMotions")
{
m_roleMotions = child as XmlElement;
}
else if (child.Name == "skills")
{
m_skills = child as XmlElement;
}
} Debug.Log("m_roleMotions = " + m_roleMotions.InnerXml);
Debug.Log("m_skills = " + m_skills.InnerXml);
} void ReadFileToXml()
{
string filepath = "1013000";
GameObject obj = Resources.Load(filepath) as GameObject;
TextAsset xmlAsset = Resources.Load(filepath,typeof(TextAsset)) as TextAsset; XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xmlAsset.text); //获取全部子节点;
XmlNodeList nodeList = xmlDoc.SelectSingleNode("character").ChildNodes;
foreach (XmlNode child in nodeList)
{
if (child.Name == "roleMotions")
{
m_roleMotions = child as XmlElement;
}
else if (child.Name == "skills")
{
m_skills = child as XmlElement;
}
} Debug.Log("m_roleMotions = " + m_roleMotions.InnerXml);
Debug.Log("m_skills = " + m_skills.InnerXml);
} }

新建的Xml文档内容例如以下:

<character>
<roleMotions>
<motionInfo>
<motion clipName="enter_ready" isLoop="false" moveEndTime="0" moveStartTime="0" />
</motionInfo>
</roleMotions>
<skills>
<skill name="普攻" motion="RMT_Attack1" />
</skills>
</character>

读取Xml结果:

最新文章

  1. [收集]MVC3 HTML辅助方法集录
  2. 数据库连接池配置 - Oracle,SQL Server,DB2,MYSQL,SQLLITE3
  3. osmocom-bb中用osmocon刷入固件命令那些参数你都弄懂了吗?
  4. 通过asp.net程序来控制自己开发的windows服务
  5. collectionView关于点击事件
  6. WPF 采用Border创建圆角
  7. ASP.NET Core 运行原理解剖[1]:Hosting
  8. Struts功能详解——ActionMapping对象
  9. 【全文转载】Gradle、Maven项目相互转换
  10. swift 学习- 19 -- 可选链式调用
  11. [转]centos安装autossh
  12. Python特色数据类型(列表)(上)
  13. Mysql BLOB、BLOB与TEXT区别及性能影响、将BLOB类型转换成VARCHAR类型
  14. PID控制動手玩玩看
  15. Qt Creator + MinGW 在windows 下的调试GDB停止工作解决
  16. 你必须知道的10个提高Canvas性能技巧
  17. [ActionScript 3.0] 动态绘制扇形实例(拖拽绘制)
  18. Discuz常见小问题2-如何清空,删除,清除全部DIY的数据
  19. Yii2视频
  20. SVN安装中遇到的问题

热门文章

  1. 你所在的公司是如何实施DEVOPS的?
  2. bzoj 3456 城市规划 无向简单连通图个数 多项式求逆
  3. 【Hihocoder1636】Pangu and Stones(区间DP)
  4. luogu 1325 雷达安装
  5. Firmware 加载原理分析【转】
  6. AC日记——网络最大流 洛谷 P3376
  7. LibieOJ 6170 字母树 (Trie)
  8. [BZOJ5286][洛谷P4425][HNOI2018]转盘(线段树)
  9. 用Redis存储Tomcat集群的Session(转载)
  10. win7 32 c++环境