Unity中的大部分资源在发布时都被整合到工程中,我们不能访问。但有时我们需要通过路径名访问放在目标设备中的文件。这些文件被存储在目标设备的文件系统中。
在Unity工程中, 放在StreamingAssets文件夹中的任何资源都将被原样复制到目标设备上的一个特定文件夹中。在任何平台中总可以统一使用Application.streamingAssetsPath 属性来获得这一文件夹路径。.
这个文件夹是跟Application.dataPath相关联的
在 desktop computer (Mac OS or Windows) 中该文件夹为为:-
path = Application.dataPath + "/StreamingAssets";
在 iOS中, 该文件夹为:-
path = Application.dataPath + "/Raw";
在 Android中, 该文件夹为:-
path = "jar:file://" + Application.dataPath + "!/assets/";
虽然我们可以用上述三个文件夹,在各个平台中获取StreamingAssets的位置,但是最好还是统一使用Application.streamingAssetsPath.
注意,在Android中,StreamingAssets中的文件包含在一个.jar压缩文件(基本上与标准的zip为统一格式)中,所以只能用WWW读取,否则要使用第三方软件来读取,不能用C#的文件读取方法。
[csharp] view
plaincopy
if(Application.platform==RuntimePlatform.Android)
{
url=Application.streamingAssetsPath+"/brick.jpg";//在Android中实例化WWW不能在路径前面加"file://"
path=Application.streamingAssetsPath+"/1.txt";//1前面的“/”必须要有,否则会出错
// FileInfo fileinfo=new FileInfo(path); 不能在Android中使用
// StreamReader r=fileinfo.OpenText();
WWW wWA=new WWW(path);///WWW读取在各个平台上都可使用
yield return wWA;
line1=wWA.text;
}
else
{
url="file://"+Application.streamingAssetsPath+"/brick.jpg";//在Windows中实例化WWW必须要在路径前面加"file://"
path=Application.streamingAssetsPath+"/1.txt";//1前面的“/”必须要有,否则会出错
WWW wWA=new WWW("file://"+path);
yield return wWA;
line1=wWA.text;
//FileInfo fileinfo=new FileInfo(path);可以在Android以外的平台使用
//文件读取的路径中不要加“file://”。总结:只有在Windows中实例化WWW时,才需要加"file://",有时貌似要三个斜杠"///":Note: http://, https:// and file:// protocols are supported on iPhone. ftp:// protocol support is limited to anonymous downloads only. Other protocols are not supported.
//''Note:'' When using file protocol on Windows and Windows Store Apps for accessing local files, you have to specify file:///__ (with three slashes).
// StreamReader r=fileinfo.OpenText();
//
// while((line=r.ReadLine())!=null)
// {
// line1=line;
// Debug.Log(line);TextField
// }
}
下面就是对一个APK文件使用WINRAR解压后得到的
StreamingAssets中的文件(1.txt和brick.jpg)就存储在assets文件夹下面,也就是说这个assets文件夹就对应于Unity编辑器中的StreamingAssets文件夹。
---------------------
本文来自 SahaLi 的CSDN 博客 ,全文地址请点击:https://blog.csdn.net/ldghd/article/details/36013839?utm_source=copy