我们在读写例如XML和TXT文件的时候,在电脑上和手机上路径不一致,造成了很多麻烦,其实有个简单的方法,在项目工程中新建一个StreamingAssets文件夹,把你的XML和TXT文件放到这里。文章来源:https://www.toymoban.com/news/detail-588431.html
注:其实每个平台的路径都可以是Application.streamingAssetsPath+“/Achievement.xml”。但是android平台必须要用WWW加载。文章来源地址https://www.toymoban.com/news/detail-588431.html
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
{ //Unity3D教程手册:www.unitymanual.com
[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();
} //Unity3D教程手册:www.unitymanual.com
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);
}
}
到了这里,关于Unity3D教程:Streaming Assets路径的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!