- using UnityEngine;
- using System.Collections;
- using System.Xml;
- using System.Xml.Serialization;
- using System.IO;
- using System.Text;
- public class Reward
- {
- public int taskNo;
- public Task[] task = new Task[15];
- public Attribute attribute;
- public Reward () {}
- public struct Task
- {
- [XmlAttribute("taskReward")]
- public string taskReward{ get; set;}
- public Id id1;
- public Id id2;
- public Id id3;
- }
- public struct Id
- {
- [XmlAttribute("flag")]
- public bool flag{ get; set;}
- [XmlAttribute("name")]
- public string name{ get; set;}
- [XmlText()]
- public string description{get;set;}
- }
- }
- public class AchievementManager: MonoBehavIoUr {
- Reward reward ;
- FileInfo fileInfo;
- string _data;
- void Start ()
- {
- reward = new Reward();
- LoadXML();
- }
- void LoadXML()
- {
- if(Application.platform == RuntimePlatform.IPhonePlayer)
- {
- fileInfo = new FileInfo(Application.dataPath + "/Raw/" + "Achievement.xml");
- StreamReader r = fileInfo.OpenText();
- _data = r.ReadToEnd();
- r.Close();
- }
- else if(Application.platform == RuntimePlatform.Android)
- {
- fileInfo = new FileInfo(Application.streamingAssetsPath+"/Achievement.xml");
- StartCoroutine("LoadWWW");
- }
- else
- {
- fileInfo = new FileInfo(Application.dataPath + "/StreamingAssets/"+ "Achievement.xml");
- StreamReader r = fileInfo.OpenText();
- _data = r.ReadToEnd();
- r.Close();
- }
- if(_data.ToString() != "")
- {
- reward = (Reward)DeserializeObject(_data);
- }
- }
- void OnGUI()
- {
- GUI.Label(new Rect(0,Screen.width,Screen.height),"data:"+_data);
- if(Input.GetKey(KeyCode.Space))
- {
- Application.Quit();
- }
- }
- IEnumerator LoadWWW()
- {
- WWW www = new WWW(Application.streamingAssetsPath+"/Achievement.xml");
- yield return www;
- _data =www.text;
- }
- public void Save()
- {
- _data = SerializeObject(reward);
- StreamWriter writer;
- fileInfo.Delete();
- writer = fileInfo.CreateText();
- writer.Write(_data);
- writer.Close();
- }
- string UTF8ByteArrayToString(byte[] characters)
- {
- UTF8Encoding encoding = new UTF8Encoding();
- string constructedString = encoding.GetString(characters);
- return (constructedString);
- }
- byte[] StringToUTF8ByteArray(string pXmlString)
- {
- UTF8Encoding encoding = new UTF8Encoding();
- byte[] byteArray = encoding.GetBytes(pXmlString);
- return byteArray;
- }
- // Here we serialize our Reward object of reward
- string SerializeObject(object pObject)
- {
- string XmlizedString = null;
- MemoryStream memoryStream = new MemoryStream();
- XmlSerializer xs = new XmlSerializer(typeof(Reward));
- XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream,Encoding.UTF8);
- xs.Serialize(xmlTextWriter,pObject);
- memoryStream = (MemoryStream)xmlTextWriter.BaseStream;
- XmlizedString = UTF8ByteArrayToString(memoryStream.ToArray());
- return XmlizedString;
- }
- // Here we deserialize it back into its original form
- object DeserializeObject(string pXmlizedString)
- {
- XmlSerializer xs = new XmlSerializer(typeof(Reward));
- MemoryStream memoryStream = new MemoryStream(StringToUTF8ByteArray(pXmlizedString));
- XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream,Encoding.UTF8);
- return xs.Deserialize(memoryStream);
- }
- }
我们在读写例如XML和TXT文件的时候,在电脑上和手机上路径不一致,造成了很多麻烦,其实有个简单的方法,在项目工程中新建一个StreamingAssets文件夹,把你的XML和TXT文件放到这里。
注:其实每个平台的路径都可以是Application.streamingAssetsPath+"/Achievement.xml"。但是android平台必须要用WWW加载,其他的平台貌似也可以的