unity中的streaming文件路径

	IEnumerator test1(){
		String filePath1=Application.streamingAssetsPath+"/Boss.bank";
		String filePath2="file:///"+Application.streamingAssetsPath+"/Boss.bank";
		String filePath3=System.IO.Path.Combine(Application.streamingAssetsPath,"/Boss.bank");
		String filePath4=System.IO.Path.Combine(Application.streamingAssetsPath,"Boss.bank");


		textbox1.label.text=filePath1+"\n\n"+filePath2+"\n\n"+filePath3+"\n\n"+filePath4+"\n\n";

		textbox1.label.text+=File.Exists(filePath1)+"\n";
		textbox1.label.text+=File.Exists(filePath2)+"\n";
		textbox1.label.text+=File.Exists(filePath3)+"\n";
		textbox1.label.text+=File.Exists(filePath4)+"\n";

		WWW www1=new WWW(filePath1);
		yield return www1;

		WWW www2=new WWW(filePath2);
		yield return www2; 

		WWW www3=new WWW(filePath3);
		yield return www3;

		WWW www4=new WWW(filePath4);
		yield return www4;

		textbox1.label.text+=www1.bytesDownloaded+" "+www1.error+"\n";
		textbox1.label.text+=www2.bytesDownloaded+" "+www2.error+"\n";		
		textbox1.label.text+=www3.bytesDownloaded+" "+www3.error+"\n";		
		textbox1.label.text+=www4.bytesDownloaded+" "+www4.error+"\n";
	}

 

PC 运行如图:

android手机运行如图:

如果在unity中取文件:

string filePath = System.IO.Path.Combine(Application.streamingAssetsPath, "csv/"+configName);
		byte[] result=null;
		if (filePath.Contains("://")) {
			WWW www = new WWW(filePath);
			yield return www;
                        while(!www.isDone()){
                             yield return null;
                        }
			result = www.bytes;
		} else {
			result = System.IO.File.ReadAllBytes(filePath);
		}

如果在unity中想全部用www取文件

string filePath3 = Application.streamingAssetsPath + "/bt2/android/";
         if (!filePath3.Contains("://"))
         {
            filePath3 = "file:///" + filePath3;
         }

在android下需要注意,因为apk本身是压缩格式,无法直接用File取文件,必须用www取文件。

用www取文件注意格式,一定要等www.isDone

发表评论?

1 条评论。

  1. 请问博主,我在unity里面访问安卓手机本地的一张图片,能不能使用file.readallbytes?

回复给 羁绊 ¬
取消回复


注意 - 你可以用以下 HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>


Warning: Use of undefined constant XML - assumed 'XML' (this will throw an Error in a future version of PHP) in /opt/wordpress/wp-content/plugins/wp-syntaxhighlighter/wp-syntaxhighlighter.php on line 1048