• QQ
  • nahooten@sina.com
  • 常州市九洲新世界花苑15-2

游戏开发

常州微信公众号游戏开发-U3D读取XML文件里的属性

原创内容,转载请注明原文网址:http://homeqin.cn/a/wenzhangboke/jishutiandi/youxikaifa/2019/0114/334.html

常州微信公众号游戏开发-Unity3D读取XML文件里面的属性

 
 
之前给大家介绍过Unity读取XML、JSON文件的方法,而App开发培训本篇要再深入一下, 给大家介绍的是读取XML文件里面的属性,不清楚的可以看看。

using Mono.Xml;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Security;
using System.Xml;
using UnityEngine;
public class ReadXML : MonoBehaviour {
    // Use this for initialization
    void Start () {
        //TextAsset ta = Resources.Load("test", typeof(TextAsset)) as TextAsset;
        //ReadXMlTest(new MemoryStream(ta.bytes));
        StartCoroutine(StartTxt());
    }
    // Update is called once per frame
    void Update () {
    }
    IEnumerator StartTxt()
    {
        WWW www = new WWW("file://" + Application.streamingAssetsPath + "/test.xml");
        yield return www;
        //ReadXMlTest(new MemoryStream(www.bytes));
        ReadXMLMono(www.text);
        www.Dispose();
    }
    void ReadXMlTest(Stream stream)
    {
        XmlDocument xmldoc = new XmlDocument();
        xmldoc.Load(stream);
        XmlNode info = xmldoc.SelectSingleNode("info");
        foreach (XmlNode node in info.ChildNodes)
        {
            string id = node.Attributes["id"].Value;
            string lang = node.Attributes["lang"].Value;
            string name = node.SelectSingleNode("name常州网站开发培训").InnerText;
            string price = node.SelectSingleNode("price").InnerText;
            Debug.Log("node.Name:" + node.Name + " id:"+ id + " lang:" + lang + " name:" + name + " price:" + price);
        }
    }
    void ReadXMLMono(string text)
    {
        SecurityParser sp = new SecurityParser();
        sp.LoadXml(text);
        SecurityElement se = sp.ToXml();
        foreach (SecurityElement sel in se.Children)
        {
            string id = (string)sel.Attributes["id"];
            string lang = (string)sel.Attributes["lang"];
            string name = "", price = "";
            foreach (SecurityElement se2 in sel.Children)
            {
                if (se2.Tag == "name")
                {
                    name = se2.Text;
                }
                else if (se2.Tag == "price")
                {
                    price = se2.Text;
                }
            }
            Debug.Log(" id:" + id + " lang:" + lang + " name:" + name + " price:" + price);
        }
    }

上篇:上一篇:常州手游开发-U3D手游资源打包Assetbundle
下篇:下一篇:常州手游开发制作-Unity3D中读取json文件