LoadXml

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;
using UnityEngine.UI;
using System.Xml;
using System.IO;

public class LoadXml : MonoBehaviour
{
    #region 定义变量
    string mm = "jar:file://";
    string nn = "file://";
    string configPathName;
    string AwardconfigPathName;
    FileInfo myFileInfo;
    FileInfo AwardmyFileInfo;
    Dictionary<string, int> TeamNumDic = new Dictionary<string, int>();
    Dictionary<string, Texture2D> Texture2DList = new Dictionary<string, Texture2D>();
    List<XmlElement> eles = new List<XmlElement>();
    List<XmlElement> Awardeles = new List<XmlElement>();
    Dictionary<string, AwardInfo> myAwardTexture2DDic = new Dictionary<string, AwardInfo>();
    string Name;
    string Path;
    string beforePath;
    int TeamNum;
    string beforeAwardPath;
    #endregion
    IEnumerator Start()
    {
        beforePath = "file://" + "D:\\Images\\";//疑问:"D:\\Images\\"和"D:/Images/"有何区别,分别应用哪种情形中?
        beforePath = "file://" + "C:/Users/Administrator/Desktop/Temp/7.7/Images/";
        beforePath = "file://" + "C:/Users/Administrator/Desktop/Temp/7.8/VR_Practice_Museum/Images/Dashiji/";
        beforeAwardPath = "file://" + "C:/Users/Administrator/Desktop/Temp/7.8/VR_Practice_Museum/Images/Lubanjiang/";
        configPathName = "TextureXML.xml";
        AwardconfigPathName = "AwardTextureXML.xml";
        #region 资源路径 各平台之间的区分
        string path =
#if UNITY_ANDROID && !UNITY_EDITOR
                            Application.streamingAssetsPath + "/";
#elif UNITY_IPHONE && !UNITY_EDITOR
                            Application.dataPath+"/Raw/"+ configPathName;
#elif UNITY_STANDALONE_WIN || UNITY_EDITOR
                            //nn + Application.dataPath + "/streamingAssets/" + configPathName;
                            Application.streamingAssetsPath + "/";
#else
                            string.Empty;
#endif
        #endregion
        #region 临时文件 各平台文件路径区分(未使用)
        //        string PathTexture2D =
        //#if UNITY_ANDROID && !UNITY_EDITOR
        //                            Application.streamingAssetsPath + "/AirplaneTextures/";
        //#elif UNITY_IPHONE && !UNITY_EDITOR
        //                            Application.dataPath+"/Raw/"+ configPathName;
        //#elif UNITY_STANDALONE_WIN || UNITY_EDITOR
        //                           nn + Application.dataPath + "/streamingAssets/AirplaneTextures/";
        //#else
        //                            string.Empty;
        //#endif
        #endregion
        myFileInfo = new FileInfo(path + configPathName);
        AwardmyFileInfo = new FileInfo(path + AwardconfigPathName);
        #region 加载TextureXML.xml
        if (myFileInfo != null)
        {
            #region XMLReader.Create方式加载资源(推荐)
            XmlDocument myxmldoc = new XmlDocument();
            XmlReaderSettings set = new XmlReaderSettings();
            set.IgnoreComments = true;//忽视xml的注释文档
            myxmldoc.Load(XmlReader.Create(path + configPathName, set));
            #endregion
            #region WWW方式加载资源(未使用)
            //myxmldoc.Load(XmlReader.Create(Path));
            //myxmldoc.LoadXml();
            //WWW www = new WWW(Path);
            //yield return www;
            //myxmldoc.LoadXml(www.text);
            #endregion
            XmlNodeList myxmlNodeList = myxmldoc.SelectSingleNode("configuration").ChildNodes;
            #region 赋值到字典里
            foreach (XmlElement ele in myxmlNodeList)
            {
                eles.Add(ele);
            }
            for (int i = 0; i < eles.Count; i++)
            {
                if (eles[i].GetAttribute("id") == (i + 1).ToString())
                {
                    foreach (XmlElement eleChild in eles[i])
                    {
                        switch (eleChild.GetAttribute("name"))
                        {
                            case "Name":
                                Name = eleChild.InnerText;
                                break;
                            case "PicturePath":
                                Path = eleChild.InnerText;
                                break;
                            case "Team":
                                TeamNum = int.Parse(eleChild.InnerText);
                                break;
                        }
                    }
                    TeamNumDic.Add(Name, TeamNum);
                    WWW www = new WWW(beforePath + Path);
                    yield return www;
                    Texture2D texture1 = www.texture;
                    if (www != null && string.IsNullOrEmpty(www.error))
                    {
                        Texture2DList.Add(Name, texture1);
                    }
                }
            }
            #endregion
            Manager.Instance.GetTexture2DList(Texture2DList);
            Manager.Instance.GetTeamNumDic(TeamNumDic);
        }
        else
        {
            Debug.Log("DataLoad内输出文件不存在!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
        }
        #endregion
        #region 加载AwardTextureXML.xml
        if (AwardmyFileInfo != null)
        {
            #region XMLReader.Create方式加载资源(推荐)
            //myxmldoc = new XmlDocument();
            //XmlReaderSettings set = new XmlReaderSettings();
            //set.IgnoreComments = true;//忽视xml的注释文档
            //myxmldoc.Load(XmlReader.Create(path, set));
            #endregion
            #region WWW方式加载资源(使用)
            XmlDocument myxmldoc = new XmlDocument();
            WWW www = new WWW(nn + path + AwardconfigPathName);
            yield return www;
            myxmldoc.LoadXml(www.text);
            #endregion
            XmlNodeList myxmlNodeList = myxmldoc.SelectSingleNode("configuration").ChildNodes;
            #region 赋值到字典里
            foreach (XmlElement ele in myxmlNodeList)
            {
                Awardeles.Add(ele);
            }
            print("Awardeles.Count=" + Awardeles.Count);
            for (int i = 0; i < Awardeles.Count; i++)
            {
                AwardInfo myAwardInfo = new AwardInfo();
                foreach (XmlElement eleChild in Awardeles[i])
                {
                    #region switch
                    switch (eleChild.GetAttribute("name"))
                    {
                        case "Id":
                            myAwardInfo.ID = eleChild.InnerText;
                            break;
                        case "PicName0":
                            myAwardInfo.PicName = eleChild.InnerText;
                            www = new WWW(beforeAwardPath + myAwardInfo.PicName);
                            yield return www;
                            Texture2D texture1 = www.texture;
                            if (www != null && string.IsNullOrEmpty(www.error))
                            {
                                myAwardInfo.Sprite0 = Sprite.Create(texture1, new Rect(0, 0, texture1.width, texture1.height), Vector2.zero);
                            }
                            break;
                        case "PicName1":
                            myAwardInfo.PicName = eleChild.InnerText;
                            www = new WWW(beforeAwardPath + myAwardInfo.PicName);
                            yield return www;
                            texture1 = www.texture;
                            if (www != null && string.IsNullOrEmpty(www.error))
                            {
                                myAwardInfo.Sprite1 = Sprite.Create(texture1, new Rect(0, 0, texture1.width, texture1.height), Vector2.zero);
                            }
                            break;
                        case "PicName2":
                            myAwardInfo.PicName = eleChild.InnerText;
                            www = new WWW(beforeAwardPath + myAwardInfo.PicName);
                            yield return www;
                            texture1 = www.texture;
                            if (www != null && string.IsNullOrEmpty(www.error))
                            {
                                myAwardInfo.Sprite2 = Sprite.Create(texture1, new Rect(0, 0, texture1.width, texture1.height), Vector2.zero);
                            }
                            break;
                        case "PicName3":
                            myAwardInfo.PicName = eleChild.InnerText;
                            www = new WWW(beforeAwardPath + myAwardInfo.PicName);
                            yield return www;
                            texture1 = www.texture;
                            if (www != null && string.IsNullOrEmpty(www.error))
                            {
                                myAwardInfo.Sprite3 = Sprite.Create(texture1, new Rect(0, 0, texture1.width, texture1.height), Vector2.zero);
                            }
                            break;
                    }
                    #endregion
                }
                myAwardTexture2DDic.Add(myAwardInfo.ID, myAwardInfo);
            }
            Manager.Instance.GetmyAwardInfoDic(myAwardTexture2DDic);
            #endregion
        }
        #endregion
    }
}
public class AwardInfo
{
    public string ID;
    public string PicName;
    public Sprite Sprite0;
    public Sprite Sprite1;
    public Sprite Sprite2;
    public Sprite Sprite3;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值