标签:
<?xml version="1.0" encoding="UTF-8" ?> <rank> <rank id="5618" score="12" ></rank> <rank id="1712" score="14" ></rank> </rank>
<?xml version="1.0" encoding="utf-8" ?> <rant> <M1001 SplitNames="NewPlayerLeadGame Map_split_A_01 Map_split_A_02 Map_split_A_03 Werther_room"></M1001> <M1014 SplitNames="Map_split_A_01 Werther_room SerdabGame" /> <M1016 SplitNames="Map_split_A_01 Werther_room SerdabGame MushroomOnGround" /> <M1020 SplitNames="Map_split_A_01 Werther_room SerdabGame MushroomOnGround Mushroom" /> <M1021 SplitNames="SerdabGame MushroomOnGround Mushroom" /> <M1026 SplitNames="SerdabGame MushroomOnGround Mushroom MushroomAtExit Prison" /> <M1030 SplitNames="Prison" /> <M1034 SplitNames="Map_split_B_02 Prison SheriffIndoorGame" /> <M1037 SplitNames="Map_split_B_01 Map_split_B_02 FindCatGame SheriffIndoorGame" /> <M1038 SplitNames="Map_split_B_02 FindCatGame" /> <M1250 SplitNames="FindCatGame" /> <M1254 SplitNames="Map_split_B_02 Map_split_B_03 FindCatGame" /> <M1042 SplitNames="Map_split_B_01 Map_split_B_02 Map_split_B_03" /> <M1047 SplitNames="Map_split_B_01 Map_split_B_02 Map_split_B_03 Map_split_A_03" /> <M1050 SplitNames="Map_split_B_01 Map_split_B_02 Map_split_A_03" /> <M1054 SplitNames="Map_split_B_01 Map_split_B_02 Map_split_A_03 MakeFireGameTrigger" /> <M1060 SplitNames="Map_split_B_01 Map_split_B_02 Map_split_B_03 Map_split_A_03 Map_split_C_01" /> <M1067 SplitNames="Map_split_B_02 Map_split_B_03 Map_split_C_01" /> <M1074 SplitNames="Map_split_B_02 Map_split_B_03 Map_split_C_01 WinehouseGame" /> <M1079 SplitNames="Map_split_B_02 Map_split_B_03 Map_split_C_01" /> <M1094 SplitNames="Map_split_B_03 Map_split_C_01 Map_split_C_02" /> <M1103 SplitNames="Map_split_B_03 Map_split_C_01 Map_split_C_02 Map_split_C_03" /> <M1110 SplitNames="Map_split_C_01 Map_split_C_02 Map_split_C_03" /> <M1111 SplitNames="Map_split_C_02 FireTheHome" /> <M1260 SplitNames="Map_split_C_02 FireTheHome HappyWithCrow Map_split_C_03" /> <M1116 SplitNames="Map_split_C_02 Map_split_C_03 Map_split_C_04" /> <M1122 SplitNames="Map_split_C_02 Map_split_C_03 Map_split_C_04 Map_split_C_01" /> <M1135 SplitNames="Map_split_C_02 Map_split_C_03 Map_split_C_04 Map_split_C_01" /> <M1140 SplitNames="Map_split_C_02 Map_split_C_03 Map_split_C_04 Werther_room" /> <M1159 SplitNames="Map_split_A_03 Map_split_A_02 Map_split_A_01 Werther_room" /> <M1165 SplitNames="Werther_room Map_split_C_02 Map_split_C_03 Map_split_C_04" /> <M1167 SplitNames="Map_split_C_02 Map_split_C_03 Map_split_C_04 Map_split_D" /> </rant>
using UnityEngine; using System.Collections; using AssemblyCSharp1; public class testinsert : MonoBehaviour { public GUIText guitext; public GUIText platform; string allscores=""; void Awake() { //if(Application.platform==RuntimePlatform.Android) } // Use this for initialization void Start () { StartCoroutine(AddressData1.GetXML()); StartCoroutine(AddressData1.GetXML2()); //AddressData1.insertNode(22); } void OnGUI() { GUI.color=Color.black; if(GUI.Button(new Rect(100,100,40,40),"load")) { foreach(int score in AddressData1.AllScore) { allscores+=score.ToString(); guitext.text+="\t"; } guitext.text = allscores; } string scortInfo=""; for(int i=0;i<AddressData1.AllScore.Count;i++){ scortInfo+=AddressData1.AllScore[i]+" "; } GUI.Label(new Rect(10,200,1000,300),scortInfo); GUI.Label(new Rect(10,400,1000,300),AddressData1.XmlContent); } // Update is called once per frame void Update () { } }
//------------------------------------------------------------------------------ // <auto-generated> // 此代码由工具生成。 // 运行时版本:4.0.30319.18063 // // 对此文件的更改可能会导致不正确的行为,并且如果 // 重新生成代码,这些更改将会丢失。 // </auto-generated> //------------------------------------------------------------------------------ using System; using UnityEngine; using System.IO; using System.Xml; using System.Linq; using System.Text; using System.Collections; using System.Collections.Generic; namespace AssemblyCSharp1 { public class AddressData1 { public string timeURL; public static string all; public static string hp; public static string speed; public static string demage; public static string localPath; public static string id; public static string score; private static List<int> allScore=new List<int>(); private static string xmlContent=""; public static string XmlContent { get { return xmlContent; } } public static List<int> AllScore { get { return allScore; } } public void AddressData () { Debug.Log (localPath); } /// <summary> /// 获取XML路径 /// </summary> /// <returns>The XM.</returns> public static IEnumerator GetXML() { if(Application.platform==RuntimePlatform.Android) { localPath = Application.streamingAssetsPath+"/score.xml"; //在Android中实例化WWW不能在路径前面加"file://" Debug.Log (localPath); } else { localPath = "file://"+UnityEngine.Application.streamingAssetsPath + "/score.xml";//在Windows中实例化WWW必须要在路径前面加"file://" Debug.Log (localPath); } WWW www = new WWW(localPath); while (!www.isDone) { Debug.Log("Getting GetXML"); yield return www; all = www.text; ParseXml(www); } } public static IEnumerator GetXML2() { if(Application.platform==RuntimePlatform.Android) { localPath = Application.streamingAssetsPath+"/Split.xml"; //在Android中实例化WWW不能在路径前面加"file://" Debug.Log (localPath); } else { localPath = "file://"+UnityEngine.Application.streamingAssetsPath + "/Split.xml";//在Windows中实例化WWW必须要在路径前面加"file://" Debug.Log (localPath); } WWW www = new WWW(localPath); while (!www.isDone) { Debug.Log("Getting GetXML"); yield return www; all = www.text; Debug.Log(all); ParseXml2(www); } } /// <summary> ///按属性获取节点 /// </summary> /// <param name="www">Www.</param> public static void ParseXml(WWW www) { if(allScore == null) { allScore =new List<int>(); } XmlDocument xmlDoc = new XmlDocument(); xmlDoc.LoadXml(www.text); XmlNodeList nodeList=xmlDoc.SelectSingleNode("rank").ChildNodes; foreach(XmlElement xe in nodeList) { id = xe.GetAttribute("id"); score = xe.GetAttribute("score"); allScore.Add(int.Parse(score)); //将所有得分读入List Debug.Log ("ID:"+id+" Score:"+score); } allScore.Sort(); allScore.Reverse(); foreach(var score in allScore ) { Debug.Log (score.ToString()); } } public static void ParseXml2(WWW www) { if(allScore == null) { allScore =new List<int>(); } XmlDocument xmlDoc = new XmlDocument(); xmlDoc.LoadXml(www.text); XmlNodeList nodeList=xmlDoc.SelectSingleNode("rant").ChildNodes; foreach(XmlElement xe in nodeList) { xmlContent += xe.GetAttribute("SplitNames"); } } /// <summary> /// 读取xml内容 /// </summary> public static IEnumerator load() { string url = string.Empty; string path = string.Empty; string line1 = string.Empty; if(Application.platform==RuntimePlatform.Android) { url=Application.streamingAssetsPath+"/hp.xml"; //在Android中实例化WWW不能在路径前面加"file://" WWW wWA=new WWW(path);///WWW读取在各个平台上都可使用 yield return wWA; line1=wWA.text; Debug.Log (line1); } else { url="file://"+Application.streamingAssetsPath+"/hp.xml";//在Windows中实例化WWW必须要在路径前面加"file://" WWW wWA=new WWW("file://"+url); yield return wWA; line1=wWA.text; Debug.Log (line1); } yield return null; } /// <summary> /// 加载xml文档 /// </summary> /// <returns></returns> public static XmlDocument ReadAndLoadXml() { XmlDocument doc = new XmlDocument(); //Debug.Log("加载xml文档"); doc.Load(localPath); return doc; } /// <summary> /// 增加节点 /// </summary> /// <returns>The node.</returns> /// <param name="score">Score.</param> public static void insertNode(int score) { int minute=int.Parse((System.DateTime.Now.Minute.ToString())); string order = System.DateTime.Now.Hour+""+System.DateTime.Now.Minute+""+System.DateTime.Now.Second; XmlDocument xmlDoc=new XmlDocument(); xmlDoc.Load(Application.dataPath + "/StreamingAssets/score.xml"); XmlNode root=xmlDoc.SelectSingleNode("rank"); XmlElement xel=xmlDoc.CreateElement("rank"); //建立节点 xel.SetAttribute("id",order); xel.SetAttribute("score",score.ToString()); root.AppendChild(xel); xmlDoc.Save(Application.dataPath + "/StreamingAssets/score.xml"); return; } } }
标签:
原文地址:http://www.cnblogs.com/unity-android-ios/p/5569689.html