zoukankan      html  css  js  c++  java
  • Unity3d 新建xml 读取xml

    在游戏开发中。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结果:


  • 相关阅读:
    世界上最受欢迎的色彩出炉了,她的名字叫马尔斯绿
    一步一步学会preload和prefetch
    chrome插件编写中需要了解的几个概念和一些方法
    SVG矢量绘图 path路径详解(贝塞尔曲线及平滑)
    为什么Object.prototype在Function的原型链上与Function.prototype在Object的原型链上都为true
    排序算法总结
    iterm2 "agnoster"主题设置中的一些踩坑 2018.8
    webpack4与babel配合使es6代码可运行于低版本浏览器
    认识JWT
    「前端进阶」彻底弄懂前端路由
  • 原文地址:https://www.cnblogs.com/blfbuaa/p/6789945.html
Copyright © 2011-2022 走看看