zoukankan      html  css  js  c++  java
  • unity创建xml与加载xml

      public void CreateConfigFile (Transform cellParent)
        {
            XmlDocument xmlDoc = new XmlDocument ();
            string rootName = "ChessBoard";
            XmlDeclaration xmldecl = xmlDoc.CreateXmlDeclaration ("1.0", "UTF-8", "");
            xmlDoc.AppendChild (xmldecl);
            XmlElement root = xmlDoc.CreateElement (rootName);
    
            UICellItem[] itemArray = cellParent.GetComponentsInChildren<UICellItem> ();
            for (int i = 0, length = itemArray.Length; i < length; i++) {
                XmlElement item = xmlDoc.CreateElement ("Item");
                item.SetAttribute ("Pos", itemArray [i].coords.ToString ());
                item.SetAttribute ("TargetPos", itemArray [i].targetPos.ToString ());
                item.SetAttribute ("Direction", _getDirection (itemArray [i].directions).ToString ());
                item.SetAttribute ("Priority", _getPriority (itemArray [i].directions));
                root.AppendChild (item);
            }
            xmlDoc.AppendChild (root);
            xmlDoc.Save (GetFilePath ());
            MainController.instance.UiDialogBoxPanel.ShowMsgOneBtn ("文件生成成功,对应路径:" + GetFilePath (), null);
        }
    
        public void LoadConfigFile (Transform cellParent)
        {
            XmlNodeList nodes;
            if (!hasFile (GetFilePath ())) {
                return;
            } else {
                XmlDocument xml = new XmlDocument ();
                xml.Load (@GetFilePath ());
                nodes = xml.SelectNodes ("ChessBoard/Item");
            }
    
            UICellItem[] itemArray = cellParent.GetComponentsInChildren<UICellItem> ();
            for (int i = 0; i < itemArray.Length; i++) {
                itemArray [i].ResetItem ();
            }
            string[] strTemp;
            for (int i = 0; i < itemArray.Length; i++) {
                strTemp = nodes [i].Attributes ["TargetPos"].Value.Split (',');
                itemArray [i].SetTargetPosInfo (strTemp);
                strTemp = nodes [i].Attributes ["Priority"].Value.Split (',');
                itemArray [i].SetItemDirections (strTemp);
            }
        }
  • 相关阅读:
    hdu5360 Hiking(水题)
    hdu5348 MZL's endless loop(欧拉回路)
    hdu5351 MZL's Border(规律题,java)
    hdu5347 MZL's chemistry(打表)
    hdu5344 MZL's xor(水题)
    hdu5338 ZZX and Permutations(贪心、线段树)
    hdu 5325 Crazy Bobo (树形dp)
    hdu5323 Solve this interesting problem(爆搜)
    hdu5322 Hope(dp)
    Lightoj1009 Back to Underworld(带权并查集)
  • 原文地址:https://www.cnblogs.com/Yellow0-0River/p/5362254.html
Copyright © 2011-2022 走看看