using System.Collections; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using UnityEngine; namespace GeneralTools { public class FileReader { /// /// 获取指定目录下 所有指定类型的文件路径 /// 例如 *.BMP|*.JPG|*.GIF|*.PNG 所有文件 *.* /// /// /// /// public static List GetPathAllFiles(string path, string filesType) { if (!Directory.Exists(path)) { Debug.Log("未找到路径" + path); return new List(); } List filesPath = new List(); string[] videotypeArr = filesType.Replace(" ", "").Split('|'); for (int i = 0; i < videotypeArr.Length; i++) { string[] dirs = Directory.GetFiles(@path, videotypeArr[i]); for (int j = 0; j < dirs.Length; j++) { filesPath.Add(dirs[j]); } } return filesPath; } /// /// 获取指定目录下 所有指定类型的文件路径,会查找子目录 /// 例如 *.BMP|*.JPG|*.GIF|*.PNG 所有文件 *.* /// /// /// /// public static List GetPathAllFilesRoot(string path, string filesType) { List filesPath = new List(); if (!Directory.Exists(path)) { Debug.Log("未找到路径" + path); } else { filesPath = GetPathAllFilesRootPrivate(path, filesType); } return filesPath; } private static List GetPathAllFilesRootPrivate(string path, string filesType) { List filesPath = new List(); string[] videotypeArr = filesType.Replace(" ", "").Split('|'); for (int i = 0; i < videotypeArr.Length; i++) { string[] dirs = Directory.GetFiles(@path, videotypeArr[i]); for (int j = 0; j < dirs.Length; j++) { filesPath.Add(dirs[j]); } } string[] childDirs = Directory.GetDirectories(path); foreach (var childDir in childDirs) { filesPath.AddRange(GetPathAllFilesRootPrivate(childDir, filesType)); } return filesPath; } /// /// 读取图片转成texture2d /// /// /// public static Texture2D GetTexture(string path) { if (!File.Exists(path)) { Debug.LogError("未找到路径" + path); return null; } FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read); fs.Seek(0, SeekOrigin.Begin); byte[] bytes = new byte[fs.Length]; fs.Read(bytes, 0, (int)fs.Length); fs.Close(); fs.Dispose(); fs = null; Texture2D t = new Texture2D(1, 1); t.LoadImage(bytes); t.name = Path.GetFileNameWithoutExtension(path); return t; } /// /// 读取路径下所有图片转成texture2d列表 /// /// /// public static List GetTextures(string path, string filesType = "*.jpg|*.png") { if (!Directory.Exists(path)) { Debug.LogError("未找到路径" + path); return new List(); } List files = GetPathAllFiles(path, filesType); if (files.Count == 0) { Debug.LogError("未找到图片" + path); return null; } List res = new List(); foreach (var item in files) { res.Add(GetTexture(item)); } return res; } /// /// 读取txt转成string /// /// /// public static string GetTxt(string path) { if (!File.Exists(path)) { Debug.LogError("未找到路径" + path); return null; } StreamReader r = new StreamReader(path, Encoding.UTF8); string s = r.ReadToEnd(); r.Close(); r.Dispose(); return s; } public static List GetTxtLine(string path) { if (!File.Exists(path)) { Debug.LogError("未找到路径" + path); return new List(); } List temp = new List(); StreamReader r = new StreamReader(path, Encoding.UTF8); string s; while ((s = r.ReadLine()) != null) { if (!string.IsNullOrEmpty(s)) { temp.Add(s); } } r.Close(); r.Dispose(); return temp; } public static void DeleteDirectory(string path) { if (!Directory.Exists(path)) return; string[] files = Directory.GetFiles(path); for (int i = 0; i < files.Length; i++) { File.Delete(files[i]); } string[] dirs = Directory.GetDirectories(path); for (int i = 0; i < dirs.Length; i++) { DeleteDirectory(dirs[i]); } Directory.Delete(path); } } }