// 打开一个二进制文件,fileName为绝对路径,返回值为文件长度 // 使用完毕后需要使用releaseFileBuffer回收文件内存 public static int openFile(string fileName, out byte[] fileBuffer, bool errorIfNull) { fileBuffer = null; try { #if !UNITY_ANDROID || UNITY_EDITOR FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read); if (fs == null) { if (errorIfNull) { UnityUtility.log("open file failed! filename : " + fileName); } return(0); } int fileSize = (int)fs.Length; fileBuffer = FrameBase.mBytesPoolThread.newBytes(getGreaterPow2(fileSize)); fs.Read(fileBuffer, 0, fileSize); fs.Close(); fs.Dispose(); return(fileSize); #else // 安卓平台如果要读取StreamingAssets下的文件,只能使用AssetManager if (startWith(fileName, FrameDefine.F_STREAMING_ASSETS_PATH)) { // 改为相对路径 fileName = fileName.Substring(FrameDefine.F_STREAMING_ASSETS_PATH.Length, fileName.Length - FrameDefine.F_STREAMING_ASSETS_PATH.Length); fileBuffer = AndroidAssetLoader.loadAsset(fileName, errorIfNull); } // 安卓平台如果要读取persistentDataPath的文件,则可以使用File else if (startWith(fileName, FrameDefine.F_PERSISTENT_DATA_PATH)) { fileBuffer = AndroidAssetLoader.loadFile(fileName, errorIfNull); } else { UnityUtility.logError("openFile invalid path : " + fileName); } return(fileBuffer.Length); #endif } catch (Exception) { UnityUtility.log("open file failed! filename : " + fileName); } return(0); }
// path为绝对路径 public static void findFiles(string path, List <string> fileList, List <string> patterns = null, bool recursive = true) { #if UNITY_ANDROID && !UNITY_EDITOR AndroidAssetLoader.findFiles(path, fileList, patterns, recursive); #else validPath(ref path); if (!isDirExist(path)) { return; } DirectoryInfo folder = new DirectoryInfo(path); FileInfo[] fileInfoList = folder.GetFiles(); int fileCount = fileInfoList.Length; int patternCount = patterns != null ? patterns.Count : 0; for (int i = 0; i < fileCount; ++i) { string fileName = fileInfoList[i].Name; // 如果需要过滤后缀名,则判断后缀 if (patternCount > 0) { for (int j = 0; j < patternCount; ++j) { if (endWith(fileName, patterns[j], false)) { fileList.Add(path + fileName); } } } // 不需要过滤,则直接放入列表 else { fileList.Add(path + fileName); } } // 查找所有子目录 if (recursive) { string[] dirs = Directory.GetDirectories(path); int count = dirs.Length; for (int i = 0; i < count; ++i) { findFiles(dirs[i], fileList, patterns, recursive); } } #endif }
// path为StreamingAssets下的相对路径,返回的路径列表为绝对路径 public static void findStreamingAssetsFiles(string path, List <string> fileList, List <string> patterns = null, bool recursive = true, bool keepAbsolutePath = false) { #if UNITY_ANDROID && !UNITY_EDITOR // 转换为相对路径 if (startWith(path, FrameDefine.F_STREAMING_ASSETS_PATH)) { path = path.Substring(FrameDefine.F_STREAMING_ASSETS_PATH.Length); } AndroidAssetLoader.findAssets(path, fileList, patterns, recursive); // 查找后的路径本身就是相对路径,如果需要保留绝对路径,则需要将路径加上 if (keepAbsolutePath) { int removeLength = FrameDefine.F_STREAMING_ASSETS_PATH.Length; int count = fileList.Count; for (int i = 0; i < count; ++i) { fileList[i] = FrameDefine.F_STREAMING_ASSETS_PATH + fileList[i]; } } #else if (!startWith(path, FrameDefine.F_STREAMING_ASSETS_PATH)) { path = FrameDefine.F_STREAMING_ASSETS_PATH + path; } findFiles(path, fileList, patterns, recursive); if (!keepAbsolutePath) { int removeLength = FrameDefine.F_STREAMING_ASSETS_PATH.Length; int count = fileList.Count; for (int i = 0; i < count; ++i) { fileList[i] = fileList[i].Substring(removeLength); } } #endif }