unity StreamingAssets路径 .

前端之家收集整理的这篇文章主要介绍了unity StreamingAssets路径 .前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Xml;
  4. using System.Xml.Serialization;
  5. using System.IO;
  6. using System.Text;
  7. public class Reward
  8. {
  9. public int taskNo;
  10. public Task[] task = new Task[15];
  11. public Attribute attribute;
  12. public Reward () {}
  13. public struct Task
  14. {
  15. [XmlAttribute("taskReward")]
  16. public string taskReward{ get; set;}
  17. public Id id1;
  18. public Id id2;
  19. public Id id3;
  20. }
  21. public struct Id
  22. {
  23. [XmlAttribute("flag")]
  24. public bool flag{ get; set;}
  25. [XmlAttribute("name")]
  26. public string name{ get; set;}
  27. [XmlText()]
  28. public string description{get;set;}
  29. }
  30. }
  31.  
  32. public class AchievementManager: MonoBehavIoUr {
  33. Reward reward ;
  34. FileInfo fileInfo;
  35. string _data;
  36. void Start ()
  37. {
  38. reward = new Reward();
  39. LoadXML();
  40. }
  41. void LoadXML()
  42. {
  43. if(Application.platform == RuntimePlatform.IPhonePlayer)
  44. {
  45. fileInfo = new FileInfo(Application.dataPath + "/Raw/" + "Achievement.xml");
  46. StreamReader r = fileInfo.OpenText();
  47. _data = r.ReadToEnd();
  48. r.Close();
  49. }
  50. else if(Application.platform == RuntimePlatform.Android)
  51. {
  52. fileInfo = new FileInfo(Application.streamingAssetsPath+"/Achievement.xml");
  53. StartCoroutine("LoadWWW");
  54. }
  55. else
  56. {
  57. fileInfo = new FileInfo(Application.dataPath + "/StreamingAssets/"+ "Achievement.xml");
  58. StreamReader r = fileInfo.OpenText();
  59. _data = r.ReadToEnd();
  60. r.Close();
  61. }
  62. if(_data.ToString() != "")
  63. {
  64. reward = (Reward)DeserializeObject(_data);
  65. }
  66. }
  67. void OnGUI()
  68. {
  69. GUI.Label(new Rect(0,Screen.width,Screen.height),"data:"+_data);
  70. if(Input.GetKey(KeyCode.Space))
  71. {
  72. Application.Quit();
  73. }
  74. }
  75. IEnumerator LoadWWW()
  76. {
  77. WWW www = new WWW(Application.streamingAssetsPath+"/Achievement.xml");
  78. yield return www;
  79. _data =www.text;
  80. }
  81. public void Save()
  82. {
  83. _data = SerializeObject(reward);
  84. StreamWriter writer;
  85. fileInfo.Delete();
  86. writer = fileInfo.CreateText();
  87. writer.Write(_data);
  88. writer.Close();
  89. }
  90. string UTF8ByteArrayToString(byte[] characters)
  91. {
  92. UTF8Encoding encoding = new UTF8Encoding();
  93. string constructedString = encoding.GetString(characters);
  94. return (constructedString);
  95. }
  96. byte[] StringToUTF8ByteArray(string pXmlString)
  97. {
  98. UTF8Encoding encoding = new UTF8Encoding();
  99. byte[] byteArray = encoding.GetBytes(pXmlString);
  100. return byteArray;
  101. }
  102. // Here we serialize our Reward object of reward
  103. string SerializeObject(object pObject)
  104. {
  105. string XmlizedString = null;
  106. MemoryStream memoryStream = new MemoryStream();
  107. XmlSerializer xs = new XmlSerializer(typeof(Reward));
  108. XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream,Encoding.UTF8);
  109. xs.Serialize(xmlTextWriter,pObject);
  110. memoryStream = (MemoryStream)xmlTextWriter.BaseStream;
  111. XmlizedString = UTF8ByteArrayToString(memoryStream.ToArray());
  112. return XmlizedString;
  113. }
  114. // Here we deserialize it back into its original form
  115. object DeserializeObject(string pXmlizedString)
  116. {
  117. XmlSerializer xs = new XmlSerializer(typeof(Reward));
  118. MemoryStream memoryStream = new MemoryStream(StringToUTF8ByteArray(pXmlizedString));
  119. XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream,Encoding.UTF8);
  120. return xs.Deserialize(memoryStream);
  121. }
  122. }

我们在读写例如XML和TXT文件的时候,在电脑上和手机上路径不一致,造成了很多麻烦,其实有个简单的方法,在项目工程中新建一个StreamingAssets文件夹,把你的XML和TXT文件放到这里。

注:其实每个平台的路径都可以是Application.streamingAssetsPath+"/Achievement.xml"。但是android平台必须要用WWW加载,其他的平台貌似也可以的

猜你在找的XML相关文章