原地址:http://blog.csdn.net/nateyang/article/details/8493791

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

using UnityEngine;
using System.Collections;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
using System.Text;
using System; public class Reward
{
public int taskNo; public Task[] task = new Task[];
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(,,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加载,其他的平台貌似也可以的,自己试试哈,呵呵~~~

unity StreamingAssets路径的更多相关文章

  1. unity Android在streamingAssets路径下文件无法读取的的解决方法

    unity Android在streamingAssets路径下文件,有时候plugin下的.jar或者.so无法直接读取: 解决方法之一,拷贝至其他路径: #if UNITY_ANDROID str ...

  2. Unity特殊路径

    Resources: Resources文件可以在根目录下,也可以在子目录下,只要叫Resources就好.Resources目录下所有资源将被打包进游戏存放资源的archive中,Resources ...

  3. Unity读取StreamingAssets路径下的文件

    /// <summary> ///读取StreamingAssets中的文件 /// </summary> /// <param name="path" ...

  4. Unity相关路径

    Application.dataPath 只读 在项目根目录下读取文件,但移动端没有访问权限.一般适用于PC端调试用. Application.streamingAssetsPath 在Assets目 ...

  5. unity组件路径自动生成

    unity 有时候找路径太麻烦 写了一个自动生成脚本的工具 using System.Collections.Generic; using System.IO; using System.Text; ...

  6. Unity存储路径

    一.在项目根目录中创建Resources文件夹来保存文件 可以使用Resources.Load("文件名字,注:不包括文件后缀名");把文件夹中的对象加载出来注:此方可实现对文件实 ...

  7. unity绝对路径与相对路径转化

    绝对路径->相对路径 string mp =“H:\unity(project)\New Unity Project\Assets\111.mat”; mp = mp.Substring(mp. ...

  8. unity文件路径

    转载自:https://blog.csdn.net/linxinfa/article/details/51679528 各平台具体路径: 1.Resources Resources文件夹是一个只读的文 ...

  9. Unity Android路径及注意事项

    Application.temporaryCachePath==/storage/emulated/0/Android/data/com.***.***/cache Application.persi ...

随机推荐

  1. EF实体框架之CodeFirst六

    上午的时候把复杂类型学习了一下,想着趁着周六日把Code First学习完,所以下午还是把Code First中的关系学习下.在数据库中最重要的恐怕就是E-R图了,E-R体现了表与表直接的关系.使用C ...

  2. flatbuffers 使用问题记录

    1. 命名空间的问题 ----------------------------- namespace 1.0.3 版本包含文件类型前面不需要加命名空间,但是1.1.0 中包含需要在类型前加命名空间 i ...

  3. grootJs 系统常用API接受

    groot.absUrl(url) 把相对路径转换为绝对路径 groot.model 把vm对象转换为json 去掉系统生成的的属性groot.model groot.log 输出到控制台 兼容低版本 ...

  4. 云计算之路-阿里云上:2014年6月12日12点IIS请求到达量突降

    今天中午12:00左右,在Windows性能监视器中突然发现SLB中的两台云服务器的IIS请求到达量(ArriveRate)突然下降,见下图: IIS日志中的情况如下: 综合以上情况,我们推测在12: ...

  5. Qt程序启动画面播放(gif与swf两种动画格式)

    学习Qt有一段时间了,发现一个小问题,网上关于Qt的资料或者总结性的学习及应用文章有点少. 比如,Qt完整的API,程序运行之前的启动画面如何按理想效果播放等,每次想在项目中添加一些应用的时候,总是找 ...

  6. [USACO2002][poj1947]Rebuilding Roads(树形dp)

    Rebuilding RoadsTime Limit: 1000MS Memory Limit: 30000KTotal Submissions: 8589 Accepted: 3854Descrip ...

  7. 【The final】软件工程实践总结

    软件工程就这么告一段落了,竟然有那么一丢丢的舍不得-- 一.为拖延找的种种借口     [首先声明]以下纯粹是个人吐槽,仅作记录以便日后自己可以回顾一下往昔罢了,可以直接忽略,跳到第二大点:我的拖延之 ...

  8. Xcode7企业版打包

    今天才发现Xcode7企业账号打包竟然和以前稍微不一样了,一时手残,先把公司服务器以前的ipa包删了,吓得我的小心脏呢 首先选中然后选archive然后点export然后然后选中include man ...

  9. Java的多线程机制系列:(四)不得不提的volatile及指令重排序(happen-before)

    一.不得不提的volatile volatile是个很老的关键字,几乎伴随着JDK的诞生而诞生,我们都知道这个关键字,但又不太清楚什么时候会使用它:我们在JDK及开源框架中随处可见这个关键字,但并发专 ...

  10. 001-编译hadoop-2.5.2总结

    前两天废了很大的劲来对hadoop-2.5.2进行64位系统的手动编译,由于对linux系统环境以及hadoop本身的不熟悉,编译过程中也出现了很多的问题,在此记录一下,对自己以后再次编译和看到此文章 ...