unity中 StreamingAssetsPath 資料夾和 PersistentDataPath 資料夾 區別
阿新 • • 發佈:2019-01-29
#define log using System.IO; using UnityEngine; using System.Collections; using System; public enum FileType { None, AudioCilp, String, Texture, } public enum FolderType { None, StreamingAssets, PersistentData, } public class FileManager: MonoBehaviour { /// <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 CopyStreamingFileToPersistentDataPath(params string[] filePath) { if (Application.platform == RuntimePlatform.Android) //如果是Android平臺 { foreach(string path in filePath) { StartCoroutine(CopyFile_Android(path)); } } else if (Application.platform == RuntimePlatform.WindowsEditor) //如果是Windows平臺 { foreach (string path in filePath) { File.Copy(Application.streamingAssetsPath + "/" + path, Application.persistentDataPath + "/" + path, true); log("CopyMusic Success!" + "\n" + "Path: ======> " + Application.persistentDataPath + "/" + path); } } } /// <summary> /// 從StreamingAssets載入檔案 /// </summary> public void LoadFileByStreamingAssets(string file,FileType fileType, Action<object> Callback) { FileType m_FileType = fileType; FolderType m_FolderType = FolderType.StreamingAssets; StartCoroutine(LoadFileIEnumerator(file, Callback, m_FileType, m_FolderType)); } /// <summary> /// 從PersistentDataPath載入檔案 /// </summary> public void LoadFileByPersistentDataPath(string file, FileType fileType, Action<object> Callback) { FileType m_FileType = fileType; FolderType m_FolderType = FolderType.PersistentData; StartCoroutine(LoadFileIEnumerator(file, Callback, m_FileType, m_FolderType)); } //=============================================================================== /// <summary> /// 載入檔案 /// </summary> IEnumerator LoadFileIEnumerator(string file,Action<object> Callback, FileType fileType, FolderType m_FolderType) { string filePath = ""; switch (m_FolderType) { case FolderType.StreamingAssets: if (Application.platform == RuntimePlatform.Android) //如果是Android平臺 { filePath = Application.streamingAssetsPath + "/" + file; } else if (Application.platform == RuntimePlatform.WindowsEditor) //如果是Windows平臺 { filePath = "file:///" + Application.streamingAssetsPath + "/" + file; } break; case FolderType.PersistentData: if (Application.platform == RuntimePlatform.Android) //如果是Android平臺 { filePath = "file://" + Application.persistentDataPath + "/" + file; } else if (Application.platform == RuntimePlatform.WindowsEditor) //如果是Windows平臺 { filePath = "file:///" + Application.persistentDataPath + "/" + file; } break; } WWW w = new WWW(filePath); yield return w; if (w.error == null) { switch (fileType) { case FileType.AudioCilp: //我使用的Unity是2017.3.0.此處 這裡只 支援 .ogg 格式的 音訊 Callback(w.GetAudioClip()); break; case FileType.String: string data = System.Text.Encoding.UTF8.GetString(w.bytes); Callback(data); break; case FileType.Texture: Callback(w.texture); break; default: break; } switch (m_FolderType) { case FolderType.StreamingAssets: log("StreamingAssetsPath Load Success..."); break; case FolderType.PersistentData: log("PersistentDataPath Load Success..."); break; } } 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) { #if log Debug.Log(content); #endif } }
之後通過下面指令碼就可以使用了 :