using System; using System.IO; using System.Threading.Tasks; using UnityEngine; using System.Collections; using UnityEngine.Networking; public class FileTool { public static void CreateFile(string fileDict, string fileName) { if (!File.Exists(fileDict)) { Directory.CreateDirectory(fileDict); } FileInfo file = new FileInfo($"{fileDict}/{fileName}"); file.Create().Dispose(); } public static void DeleteAllFile(string srcPath) { try { DirectoryInfo dir = new DirectoryInfo(srcPath); FileSystemInfo[] fileinfo = dir.GetFileSystemInfos(); //返回目录中所有文件和子目录 foreach (FileSystemInfo i in fileinfo) { if (i is DirectoryInfo) //判断是否文件夹 { DirectoryInfo subdir = new DirectoryInfo(i.FullName); subdir.Delete(true); //删除子目录和文件 } else { File.Delete(i.FullName); //删除指定文件 } } } catch (Exception e) { Debug.Log($"delete {srcPath} error"); } } public static void CreatePath(string path) { FileInfo fi = new FileInfo(path); DirectoryInfo dir = fi.Directory; if (!dir.Exists) { dir.Create(); } } /// /// 加密/解密 /// /// 文件流 // public static void Encypt(ref byte[] targetData) // { // //加密,与key异或,解密的时候同样如此 // int dataLength = targetData.Length; // for (int i = 0; i < dataLength; ++i) // { // targetData[i] = (byte) (targetData[i] ^ ApplicationConst.Encry_Key); // } // } /// /// 从本地文件中解密并加载AB包 /// /// /// // public static async Task LoadAssetBundleFromBytes(string path) // { // // Log.Debug($"path : {path}"); // byte[] content; // #if UNITY_ANDROID && !UNITY_EDITOR // UnityWebRequest getFile = UnityWebRequest.Get(path); // UnityWebRequestAsyncOperation req = getFile.SendWebRequest(); // while (!req.isDone) // { // await Task.Yield(); // } // content = getFile.downloadHandler.data; // #else // content = File.ReadAllBytes(path); // #endif // Encypt(ref content); // var load = AssetBundle.LoadFromMemoryAsync(content); // do // { // await Task.Yield(); // } while (!load.isDone); // return load.assetBundle; // } }