StreamingAsset文件夹




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 plain copy 在CODE上查看代码片 派生到我的代码片
  1.         if(Application.platform==RuntimePlatform.Android)  
  2.         {  
  3.             url=Application.streamingAssetsPath+"/brick.jpg";//在Android中实例化WWW不能在路径前面加"file://"  
  4.             path=Application.streamingAssetsPath+"/1.txt";//1前面的“/”必须要有,否则会出错  
  5.   
  6. //          FileInfo fileinfo=new FileInfo(path);  不能在Android中使用  
  7. //          StreamReader r=fileinfo.OpenText();   
  8.   
  9.             WWW wWA=new WWW(path);///WWW读取在各个平台上都可使用  
  10.             yield return wWA;   
  11.             line1=wWA.text;  
  12.   
  13.         }  
  14.         else  
  15.         {  
  16.             url="file://"+Application.streamingAssetsPath+"/brick.jpg";//在Windows中实例化WWW必须要在路径前面加"file://"  
  17.             path=Application.streamingAssetsPath+"/1.txt";//1前面的“/”必须要有,否则会出错  
  18.             WWW wWA=new WWW("file://"+path);  
  19.             yield return wWA;   
  20.             line1=wWA.text;  
  21.             //FileInfo fileinfo=new FileInfo(path);可以在Android以外的平台使用  
  22.             //文件读取的路径中不要加“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.  
  23.             //''Note:'' When using file protocol on Windows and Windows Store Apps for accessing local files, you have to specify file:///__ (with three slashes).  
  24. //          StreamReader r=fileinfo.OpenText();  
  25. //            
  26. //          while((line=r.ReadLine())!=null)  
  27. //          {  
  28. //              line1=line;  
  29. //              Debug.Log(line);TextField  
  30. //          }  
  31.         }  


 下面就是对一个APK文件使用WINRAR解压后得到的

StreamingAssets中的文件(1.txt和brick.jpg)就存储在assets文件夹下面,也就是说这个assets文件夹就对应于Unity编辑器中的StreamingAssets文件夹。





  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值