关注公众号,获取更多干货。
最近在做一个在Android平台频繁读写的一个项目,加载音乐的时候需要用WWW加载返回的w.audioClip,而读写xml的时候则需要用IO流读写。
于是我发现。。。Android平台路径的坑可真是不小啊
首先在Unity平台先看一下Windows系统下,各个文件夹的路径:
这里我把Unity里能打印的四个自带里文件夹路径都打印出来了,而其实,我常用的只有streamingAssetsPath和persistentDataPath
streamingAssetsPath是可以跟随Unity打包存在的文件夹,WWW和IO都可以访问这个文件夹下的文件,但是这个文件夹是只读文件夹,不支持修改文件和写入等操作。
persistentDataPath是可读写的文件夹,但是这个文件夹是在APK安装成功后创建的,所以无法将文件在打包前放入此文件夹。
那么接下来我们看看Android平台的这些文件夹路径都是什么样的。
一、 如果用WWW去加载文件:
在Windows端访问streamingAssetsPath文件夹则需要在前面加上”file:///”协议,而Android端则不用加file协议,因为Android端的streamingAssetsPath路径Unity会自动为他添加jar:file://。
同样,如果是persistentDataPath文件夹,在Windows端一样需要加”file:///”协议 ,而Android端的persistentDataPath文件夹Unity则没有像streamingAssetsPath一样,添加了jar:file://,所以,我们要在此路径前自己添一下file协议(PS:Android端的file协议需要在file前添加”jar:”)。
二、 如果用IO流去读写文件:
不管是Windows端还是Android端都是不需要file协议的,但是streamingAssetsPath在Android端不支持IO流访问,Windows端直接使用streamingAssetsPath即可。
而persistentDataPath路径不管是Windows端还是Android端,都可以直接使用,不需要添加file协议。
光看文字未免有些不爽,我自己也写了一个小Demo供大家参考。 以下是代码与工程文件。
using System.IO; using UnityEngine; using System.Collections; using UnityEngine.UI; public class Aries_UIManager : MonoBehaviour { public Text myConsole; /// <summary> /// 显示各个文件夹路径 /// </summary> public void showPath() { log("dataPath: ======> " + Application.dataPath); log("persistentDataPath: ======> " + Application.persistentDataPath); log("streamingAssetsPath: ======> " + Application.streamingAssetsPath); log("temporaryCachePath: ======> " + Application.temporaryCachePath); } /// <summary> /// 将StreamingAssets里的文件复制到PersistentDataPath下 /// </summary> public void copyFileToPersistentDataPath() { if (Application.platform == RuntimePlatform.Android) //如果是Android平台 { StartCoroutine(copyFile_Android("NowYouSeeMe.mp3")); StartCoroutine(copyFile_Android("Test.txt")); } else if (Application.platform == RuntimePlatform.WindowsEditor) //如果是Windows平台 { File.Copy(Application.streamingAssetsPath + "/NowYouSeeMe.mp3", Application.persistentDataPath + "/NowYouSeeMe.mp3",true); log("CopyMusic Success!" + "\n" + "Path: ======> " + Application.persistentDataPath + "/NowYouSeeMe.mp3"); File.Copy(Application.streamingAssetsPath + "/Test.txt", Application.persistentDataPath + "/Test.txt",true); log("CopyTxt Success!" + "\n" + "Path: ======> " + Application.persistentDataPath + "/Test.txt"); } } /// <summary> /// 从StreamingAssets加载背景音乐 /// </summary> public void LoadMusicByStreamingAssets() { StartCoroutine(LoadMusicByStreamingAssets_IEnumerator()); } /// <summary> /// 从PersistentDataPath加载背景音乐 /// </summary> public void LoadMusicByPersistentDataPath() { StartCoroutine(LoadMusicByPersistentDataPath_IEnumerator()); } /// <summary> /// 从StreamingAssets加载文本文件 /// </summary> public void LoadTxtByStreamingAssets() { StartCoroutine(LoadTxtByStreamingAssets_Ienumerator()); } /// <summary> /// 从PersistentDataPath加载文本文件 /// </summary> public void LoadTxtByPersistentDataPath() { FileInfo fi = new FileInfo(Application.persistentDataPath + "/Test.txt"); //判断文件是否存在 if (fi.Exists) { log("File Exists! Began To Read."); log("File Path : ======> " + Application.persistentDataPath + "/Test.txt"); StreamReader sr = fi.OpenText(); string data = sr.ReadToEnd(); log("Txt Content : ======> " + data); } else { log("<color=red>File Does Not Exist</color>"); } } //=============================================================================== IEnumerator LoadMusicByStreamingAssets_IEnumerator() { string filePath=""; if (Application.platform == RuntimePlatform.Android) //如果是Android平台 { filePath = Application.streamingAssetsPath + "/NowYouSeeMe.mp3"; } else if(Application.platform == RuntimePlatform.WindowsEditor) //如果是Windows平台 { filePath = "file:///" + Application.streamingAssetsPath + "/NowYouSeeMe.mp3"; } WWW w=new WWW(filePath); yield return w; if (w.error == null) { //获取Unity音源文件 AudioClip ac = w.audioClip; //赋值到Unity播放器 GetComponent<AudioSource>().clip = ac; //播放 GetComponent<AudioSource>().Play(); log("PlayMusic By StreamingAssetsPath..."); } else { log("Error : ======> " + w.error); } } IEnumerator LoadMusicByPersistentDataPath_IEnumerator() { string filePath = ""; if (Application.platform == RuntimePlatform.Android) //如果是Android平台 { filePath = "file://" + Application.persistentDataPath + "/NowYouSeeMe.mp3"; } else if (Application.platform == RuntimePlatform.WindowsEditor) //如果是Windows平台 { filePath = "file:///" + Application.persistentDataPath + "/NowYouSeeMe.mp3"; } WWW w = new WWW(filePath); yield return w; if (w.error == null) { //获取Unity音源文件 AudioClip ac = w.audioClip; //赋值到Unity播放器 GetComponent<AudioSource>().clip = ac; //播放 GetComponent<AudioSource>().Play(); log("PlayMusic By PersistentDataPath..."); } else { log("Error : ======> " + w.error); } } /// <summary> /// WWW加载Txt /// </summary> /// <returns></returns> IEnumerator LoadTxtByStreamingAssets_Ienumerator() { string filePath = ""; if (Application.platform == RuntimePlatform.Android) //如果是Android平台 { filePath = Application.streamingAssetsPath + "/Test.txt"; } else if (Application.platform == RuntimePlatform.WindowsEditor) //如果是Windows平台 { filePath = "file:///" + Application.streamingAssetsPath + "/Test.txt"; } WWW w = new WWW(filePath); yield return w; if (w.error == null) { log("File Exists! Began To Read."); log("File Path : ======> " + Application.streamingAssetsPath + "/Test.txt"); string data = System.Text.Encoding.UTF8.GetString(w.bytes); log("Txt Content : ======> " + data); } else { log("Error : ======> " + w.error); } } /// <summary> /// 安卓端复制文件 /// </summary> /// <param name="fileName">文件路径</param> /// <returns></returns> IEnumerator copyFile_Android(string fileName) { WWW w=new WWW(Application.streamingAssetsPath + "/" + fileName); yield return w; if (w.error == null) { FileInfo fi=new FileInfo(Application.persistentDataPath + "/" +fileName); //判断文件是否存在 if (!fi.Exists) { FileStream fs = fi.OpenWrite(); fs.Write(w.bytes,0,w.bytes.Length); fs.Flush(); fs.Close(); fs.Dispose(); log("CopyTxt Success!" + "\n" + "Path: ======> " + Application.persistentDataPath + fileName); } } else { log("Error : ======> " + w.error); } } /// <summary> /// 向我的控制台打印内容 /// </summary> /// <param name="content">内容</param> void log(string content) { myConsole.text += "\n" + content; } }本文永久链接:http://blog.csdn.net/Aries_H/article/details/61201880
工程文件下载链接:http://download.csdn.net/detail/aries_h/9777143
只是个人经验分享,如有不对的地方,还望大家指正谢谢。 如果有帮助到您,请关注一下公众号,谢谢。
