查看: 2137|回复: 2
打印 上一主题 下一主题

[经验分享] 【脚本资源】 Unity3D 读取XML中的内容代码

[复制链接]

6

主题

1

听众

197

积分

设计实习生

Rank: 2

纳金币
0
精华
0

最佳新人

跳转到指定楼层
楼主
发表于 2013-5-29 17:33:54 |只看该作者 |倒序浏览
pstatus"> 本帖最后由 艾西格亚 于 2013-5-30 01:31 编辑

前阵子做WebServer的时候读取服务器返回的XML研究好久··· 今天刚刚看到有人发了教程所以就发上来

using System.Xml;
using System.IO;
using LitJson;
public string showXml(string temp)
    {
        UnityEditor.AssetDatabase.Refresh();
        string filepath = Application.dataPath + "/my.xml";
        string tempText = "";
        print("a");
        print(File.Exists(filepath).ToString());
        if (File.Exists(filepath))
        {
            print("b");
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(filepath);//若要读网络 前面改成IEnum线程这里改网络xml地址即可
            XmlNodeList nodeList = xmlDoc.SelectSingleNode("main").ChildNodes;
            //
            foreach (XmlElement xe in nodeList)
            {
             //   Debug.Log(xe.GetAttribute("name"));
             //   Debug.Log(xe.Name);
                foreach (XmlElement x1 in xe.ChildNodes)
                {
                    if (x1.Name == temp)
                    {
                        tempText= x1.InnerText;
                        //    print(x1.InnerText);
                    }
                }

            }
            return tempText;
            print(tempText);
        }
        else
        {
            return "file not exists!";
        }
      //  return "";
    }

网络版:

IEnumerator showXmls(string temp,UILabel tempLabel)
    {
        WWW www = new WWW("http://zg.0099.com/resources/my.xml");
        yield return www;
        if(www!=null)
        {
            string tempText = "";
            if (www!=null)
            {
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load("http://zg.0099.com/resources/my.xml");
                XmlNodeList nodeList = xmlDoc.SelectSingleNode("main").ChildNodes;
                //
                foreach (XmlElement xe in nodeList)
                {
                    //   Debug.Log(xe.GetAttribute("name"));
                    //   Debug.Log(xe.Name);
                    foreach (XmlElement x1 in xe.ChildNodes)
                    {
                        if (x1.Name == temp)
                        {
                            tempText = x1.InnerText;
                            tempLabel.text = x1.InnerText;
                          //  (UILabel)tempText = x1.InnerText;
                            //    print(x1.InnerText);
                        }
                    }

                }
              yield  return tempText;
               // print(tempText);
            }
            else
            {
                yield return "www not exists!";
            }
        }
    }


,
分享到: QQ好友和群QQ好友和群 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
转播转播0 分享淘帖0 收藏收藏1 支持支持0 反对反对0
回复

使用道具 举报

2722

主题

42

听众

3万

积分

资深设计师

Rank: 7Rank: 7Rank: 7

纳金币
38268
精华
111

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

沙发
发表于 2013-5-30 16:06:40 |只看该作者
顶一个,果断的先收藏下来
回复

使用道具 举报

40

主题

0

听众

2万

积分

资深设计师

Rank: 7Rank: 7Rank: 7

纳金币
279
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

板凳
发表于 2013-6-1 00:23:09 |只看该作者
谢谢楼主分享
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

手机版|纳金网 ( 闽ICP备2021016425号-2/3

GMT+8, 2025-2-5 06:06 , Processed in 0.063840 second(s), 31 queries .

Powered by Discuz!-创意设计 X2.5

© 2008-2019 Narkii Inc.

回顶部