190 lines
6.1 KiB
C#
190 lines
6.1 KiB
C#
|
|
using System.Collections;
|
|||
|
|
using System.Collections.Generic;
|
|||
|
|
using System.IO;
|
|||
|
|
using System.Linq;
|
|||
|
|
using System.Text;
|
|||
|
|
using UnityEngine;
|
|||
|
|
|
|||
|
|
namespace GeneralTools
|
|||
|
|
{
|
|||
|
|
public class FileReader
|
|||
|
|
{
|
|||
|
|
/// <summary>
|
|||
|
|
/// 获取指定目录下 所有指定类型的文件路径
|
|||
|
|
/// 例如 *.BMP|*.JPG|*.GIF|*.PNG 所有文件 *.*
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="path"></param>
|
|||
|
|
/// <param name="filesType"></param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public static List<string> GetPathAllFiles(string path, string filesType)
|
|||
|
|
{
|
|||
|
|
if (!Directory.Exists(path))
|
|||
|
|
{
|
|||
|
|
Debug.Log("未找到路径" + path);
|
|||
|
|
return new List<string>();
|
|||
|
|
}
|
|||
|
|
List<string> filesPath = new List<string>();
|
|||
|
|
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;
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// 获取指定目录下 所有指定类型的文件路径,会查找子目录
|
|||
|
|
/// 例如 *.BMP|*.JPG|*.GIF|*.PNG 所有文件 *.*
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="path"></param>
|
|||
|
|
/// <param name="filesType"></param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public static List<string> GetPathAllFilesRoot(string path, string filesType)
|
|||
|
|
{
|
|||
|
|
List<string> filesPath = new List<string>();
|
|||
|
|
if (!Directory.Exists(path))
|
|||
|
|
{
|
|||
|
|
Debug.Log("未找到路径" + path);
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
filesPath = GetPathAllFilesRootPrivate(path, filesType);
|
|||
|
|
}
|
|||
|
|
return filesPath;
|
|||
|
|
}
|
|||
|
|
private static List<string> GetPathAllFilesRootPrivate(string path, string filesType)
|
|||
|
|
{
|
|||
|
|
List<string> filesPath = new List<string>();
|
|||
|
|
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;
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// 读取图片转成texture2d
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="path"></param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
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;
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// 读取路径下所有图片转成texture2d列表
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="path"></param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public static List<Texture2D> GetTextures(string path, string filesType = "*.jpg|*.png")
|
|||
|
|
{
|
|||
|
|
if (!Directory.Exists(path))
|
|||
|
|
{
|
|||
|
|
Debug.LogError("未找到路径" + path);
|
|||
|
|
return new List<Texture2D>();
|
|||
|
|
}
|
|||
|
|
List<string> files = GetPathAllFiles(path, filesType);
|
|||
|
|
if (files.Count == 0)
|
|||
|
|
{
|
|||
|
|
Debug.LogError("未找到图片" + path);
|
|||
|
|
return null;
|
|||
|
|
}
|
|||
|
|
List<Texture2D> res = new List<Texture2D>();
|
|||
|
|
foreach (var item in files)
|
|||
|
|
|
|||
|
|
{
|
|||
|
|
res.Add(GetTexture(item));
|
|||
|
|
}
|
|||
|
|
return res;
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// 读取txt转成string
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="path"></param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
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<string> GetTxtLine(string path)
|
|||
|
|
{
|
|||
|
|
if (!File.Exists(path))
|
|||
|
|
{
|
|||
|
|
Debug.LogError("未找到路径" + path);
|
|||
|
|
return new List<string>();
|
|||
|
|
}
|
|||
|
|
List<string> temp = new List<string>();
|
|||
|
|
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);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|