1. 程式人生 > >unity StreamingAssets路徑

unity StreamingAssets路徑

  我們在讀寫例如XML和TXT檔案的時候,在電腦上和手機上路徑不一致,造成了很多麻煩,其實有個簡單的方法,在專案工程中新建一個StreamingAssets資料夾,把你的XML和TXT檔案放到這裡。

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,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); 
   } 
}


注:其實每個平臺的路徑都可以是Application.streamingAssetsPath+"/Achievement.xml"。但是android平臺必須要用WWW載入,其他的平臺貌似也可以的,自己試試哈,呵呵~~~