Unity3d 新建xml 读取xml

前端之家收集整理的这篇文章主要介绍了Unity3d 新建xml 读取xml前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

在游戏开发中,Xml经常被用来作为技能配置、地图配置、人物动作配置等配置文件。Unity3d内置的Xml库让我们很方便地就可以新建Xml和读取Xml。

下面是一个例子,新建了一个Xml文档,并且读取它。

  1. using UnityEngine;
  2. using System.Collections;
  3. using System.IO;
  4. using System.Xml;
  5. using System.Text;
  6.  
  7. public class XmlTest : MonoBehavIoUr {
  8.  
  9. XmlElement m_roleMotions = null;//人物动作;
  10. XmlElement m_skills = null;//人物技能;
  11.  
  12. // Use this for initialization
  13. void Start () {
  14. //CreateXml();
  15. //ReadXml();
  16. ReadFileToXml();
  17. }
  18. // Update is called once per frame
  19. void Update () {
  20. }
  21.  
  22. void CreateXml()
  23. {
  24. string filepath = Application.dataPath + "/Resources/1013000.xml";
  25. if (!File.Exists(filepath))
  26. {
  27. //创建xml实例;
  28. XmlDocument xmlDoc = new XmlDocument();
  29.  
  30. //创建character;
  31. XmlElement root = xmlDoc.CreateElement("character");
  32.  
  33. /***创建roleMotions Start***/
  34. XmlElement roleMotions = xmlDoc.CreateElement("roleMotions");
  35. XmlElement motionInfo = xmlDoc.CreateElement("motionInfo");
  36. XmlElement motion = xmlDoc.CreateElement("motion");
  37. motion.SetAttribute("clipName","enter_ready");
  38. motion.SetAttribute("isLoop","false");
  39. motion.SetAttribute("moveEndTime","0");
  40. motion.SetAttribute("moveStartTime","0");
  41. motionInfo.AppendChild(motion);
  42. roleMotions.AppendChild(motionInfo);
  43. root.AppendChild(roleMotions);
  44. /***创建roleMotions End***/
  45.  
  46. /***创建skills Start***/
  47. XmlElement skills = xmlDoc.CreateElement("skills");
  48. XmlElement skill = xmlDoc.CreateElement("skill");
  49. skill.SetAttribute("name","普攻");
  50. skill.SetAttribute("motion","RMT_Attack1");
  51. skills.AppendChild(skill);
  52. root.AppendChild(skills);
  53. /***创建skills End***/
  54.  
  55. xmlDoc.AppendChild(root);
  56.  
  57. xmlDoc.Save(filepath);
  58. }
  59. else
  60. {
  61. Debug.LogError("File hava exist");
  62. }
  63. }
  64.  
  65. void ReadXml()
  66. {
  67. string filepath = Application.dataPath + "/Resources/1013000.xml";
  68. if (!File.Exists(filepath))
  69. {
  70. Debug.LogError("xml file not exist");
  71. return;
  72. }
  73. XmlDocument xmlDoc = new XmlDocument();
  74. xmlDoc.Load(filepath);
  75.  
  76. //获取所有子节点;
  77. XmlNodeList nodeList = xmlDoc.SelectSingleNode("character").ChildNodes;
  78. foreach(XmlNode child in nodeList)
  79. {
  80. if (child.Name == "roleMotions")
  81. {
  82. m_roleMotions = child as XmlElement;
  83. }
  84. else if (child.Name == "skills")
  85. {
  86. m_skills = child as XmlElement;
  87. }
  88. }
  89.  
  90. Debug.Log("m_roleMotions = " + m_roleMotions.InnerXml);
  91. Debug.Log("m_skills = " + m_skills.InnerXml);
  92. }
  93.  
  94. void ReadFileToXml()
  95. {
  96. string filepath = "1013000";
  97. GameObject obj = Resources.Load(filepath) as GameObject;
  98. TextAsset xmlAsset = Resources.Load(filepath,typeof(TextAsset)) as TextAsset;
  99.  
  100. XmlDocument xmlDoc = new XmlDocument();
  101. xmlDoc.LoadXml(xmlAsset.text);
  102.  
  103. //获取所有子节点;
  104. XmlNodeList nodeList = xmlDoc.SelectSingleNode("character").ChildNodes;
  105. foreach (XmlNode child in nodeList)
  106. {
  107. if (child.Name == "roleMotions")
  108. {
  109. m_roleMotions = child as XmlElement;
  110. }
  111. else if (child.Name == "skills")
  112. {
  113. m_skills = child as XmlElement;
  114. }
  115. }
  116.  
  117. Debug.Log("m_roleMotions = " + m_roleMotions.InnerXml);
  118. Debug.Log("m_skills = " + m_skills.InnerXml);
  119. }
  120.  
  121. }

新建的Xml文档内容如下:
  1. <character>
  2. <roleMotions>
  3. <motionInfo>
  4. <motion clipName="enter_ready" isLoop="false" moveEndTime="0" moveStartTime="0" />
  5. </motionInfo>
  6. </roleMotions>
  7. <skills>
  8. <skill name="普攻" motion="RMT_Attack1" />
  9. </skills>
  10. </character>

读取Xml结果:

猜你在找的XML相关文章