unity3d XML配置表读写

前端之家收集整理的这篇文章主要介绍了unity3d XML配置表读写前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
 //把数据读入
 public void SaveToStore()
    {
        string xmlPath = "";
#if UNITY_EDITOR
        xmlPath = Application.dataPath + "/Resources/Config/UI/" + m_draggingDataFileName;  //定义文件名字,路径
#elif UNITY_ANDROID
		xmlPath = Application.persistentDataPath + "/" + m_draggingDataFileName;
#else
		xmlPath = Application.persistentDataPath + "/" + m_draggingDataFileName;
#endif
        XmlDocument xmlDoc = new XmlDocument();
        XmlElement rootElement = xmlDoc.CreateElement("RECORDS");
        xmlDoc.AppendChild(rootElement);
        IEnumerator<int> StoreEnumerator = XMLManager.Instance.StoreObjects.Data.Keys.GetEnumerator();  //读出


        XmlElement node = xmlDoc.CreateElement("RECORDS");
        
        while(StoreEnumerator.MoveNext())
        {
            XMLDataStoreObjects current = XMLManager.Instance.StoreObjects.GetInfoById(StoreEnumerator.Current);  //定义数据类型
            node.SetAttribute("ID",StoreEnumerator.Current.ToString());                                          //把数据写入
            node.SetAttribute("Items",current.Items + "," + m_draggingObject.Data.configId);
            rootElement.AppendChild(node);
        }                                                          //XML中只有一行数据
        
        xmlDoc.Save(xmlPath);
    }

//数据读出
 private void InitializeData()
    {
        if(m_isInitData)
        {
            return;
        }

        m_isInitData = true;

        m_indexTitle2 = new List<int>();
        m_listScrollOffset = new List<List<float>>();

        m_listTitle1ItemInfos = new List<StoreTitleItem>();
        m_listTitle2ItemInfos = new List<List<StoreTitleItem>>();
//        m_dicDecorationViewInfo = new Dictionary<GameObject,StoreDecoItem>();
        m_listDecorationItemInfos = new List<List<List<StoreDecoItem>>>();

        int index1 = -1;
        int index2 = -1;

        IEnumerator<XMLDataStoreUI> enumerator = XMLManager.Instance.StoreUIConfig.Data.Values.GetEnumerator();
        IEnumerator<XMLDataStoreObjects> StoreEnumerator = XMLManager.Instance.StoreObjects.Data.Values.GetEnumerator();

        string allItemsID = "";
        while (StoreEnumerator.MoveNext())
        {
            XMLDataStoreObjects ObjInfo = StoreEnumerator.Current;
            allItemsID = ObjInfo.Items;
        }

        string[] idS = allItemsID.Split(',');

        while (enumerator.MoveNext())
        {
            XMLDataStoreUI info = enumerator.Current;
 //           XMLDataStoreObjects ObjInfo = StoreEnumerator.Current;

            if (info.Title1 != index1)
            {
                index1 = info.Title1;

                m_indexTitle2.Add(0);
                m_listScrollOffset.Add(new List<float>());

                StoreTitleItem titleItem = new StoreTitleItem();
                titleItem.m_index = index1;
                titleItem.m_titleSpriteName = info.Title1Name;

                m_listTitle1ItemInfos.Add(titleItem);

                m_listTitle2ItemInfos.Add(new List<StoreTitleItem>());

                m_listDecorationItemInfos.Add(new List<List<StoreDecoItem>>());
            }
             
            if (info.Title2 != index2)
            {
                index2 = info.Title2;

                m_listScrollOffset[index1].Add(0);

                StoreTitleItem titleItem = new StoreTitleItem();
                titleItem.m_index = index2;
                titleItem.m_titleSpriteName = info.Title2Name;

                m_listTitle2ItemInfos[index1].Add(titleItem);

                m_listDecorationItemInfos[index1].Add(new List<StoreDecoItem>());
            }

            for (int i=0; i<idS.Length; i++)
            {
                XMLDataMapDecorations decoInfo = XMLManager.Instance.MapDecorations.GetInfoById(System.Convert.ToInt32(idS[i]));
                if (decoInfo.decorationType == info.DecoType)
                {
                    StoreDecoItem itemInfo = new StoreDecoItem();
                    itemInfo.m_IDs = idS[i];
                    itemInfo.m_name = decoInfo.name;
                    m_listDecorationItemInfos[index1][index2].Add(itemInfo);
                }
            }
        }

    }

猜你在找的XML相关文章