zoukankan      html  css  js  c++  java
  • 使用WWW获取本地文件夹的XML配置文件

      Unity3D读取本地文件可以使用Resources.Load来读取放在Resources文件夹下的文件,如果不是放在该文件夹下,则可以通过WWW类来读取。

    譬如读取xml的配置文件。

    /// <summary>
        /// 读取颜色配置
        /// </summary>
        /// <returns></returns>
        public List<Color> GetColorXml()
        {
            string xmlPath = Application.dataPath + @"/Configs/Config.xml";
            List<Color> colorList = new List<Color>();
            if (File.Exists(xmlPath))
            {
                XmlDocument xmlDoc = new XmlDocument();            
                WWW www = new WWW("file:// " + xmlPath);
                while (true)
                {
                    if (www.isDone)
                    {
                        System.IO.StringReader stringReader = new System.IO.StringReader(www.text);
                        stringReader.Read(); // skip BOM 
                        xmlDoc.LoadXml(stringReader.ReadToEnd());
                        //xmlDoc.LoadXml(www.text);    
                        break;
                    }
                }
    
                //xmlDoc.Load(xmlPath);
                XmlNodeList nodeList = xmlDoc.SelectSingleNode("Config/ColorConfig").ChildNodes;
    
                //遍历每一个节点,拿节点的属性以及节点的内容
                foreach (XmlElement xe in nodeList)
                {
                    //Debug.Log("Attribute :" + xe.GetAttribute("id"));
                    //Debug.Log("NAME :" + xe.Name);
                    if (xe.Name != "Color")
                    {
                        continue;
                    }
    
                    Color c1 = new Color();
                    foreach (XmlElement x1 in xe.ChildNodes)
                    {
                        if (x1.Name == "r")
                        {
                            float.TryParse(x1.InnerText, out c1.r);
                            c1.r = c1.r > 1f ? c1.r / 255f : c1.r;
                        }
                        if (x1.Name == "g")
                        {
                            float.TryParse(x1.InnerText, out c1.g);
                            c1.g = c1.g > 1f ? c1.g / 255f : c1.g;
                        }
                        if (x1.Name == "b")
                        {
                            float.TryParse(x1.InnerText, out c1.b);
                            c1.b = c1.b > 1f ? c1.b / 255f : c1.b;
                        }
                        if (x1.Name == "a")
                        {
                            float.TryParse(x1.InnerText, out c1.a);
                            c1.a = c1.a > 1f ? c1.a / 255f : c1.a;
                        }
                    }
                    colorList.Add(c1);
                }
    
            }
            return colorList;
    
        }

    如果读取的xml文件有问题,可能是编码格式引起的,可以使用StringReader来读取代替www.text

    xml相关问题参考:http://answers.unity3d.com/questions/10904/xmlexception-text-node-canot-appear-in-this-state.html

  • 相关阅读:
    set--常见成员函数及基本用法
    [Swust OJ 1026]--Egg pain's hzf
    [HDU 1111]--Secret Code
    [Swust OJ 1139]--Coin-row problem
    [Swust OJ 781]--牛喝水
    [Swust OJ 1132]-Coin-collecting by robot
    [Swust OJ 249]--凸包面积
    HTTP 请求头中的 X-Forwarded-For
    HTTP 下载文件中文文件名在 Firefox 下乱码问题
    数据挖掘系列 (1) 关联规则挖掘基本概念与 Aprior 算法
  • 原文地址:https://www.cnblogs.com/townsend/p/4164234.html
Copyright © 2011-2022 走看看