使用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

 

转载于:https://www.cnblogs.com/townsend/p/4164234.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值