Unity common path and get all files under the target path
1. Commonly used paths
Get the absolute path where the project is located
string _path;
System.Environment.CurrentDirectory(_path);
Directory.GetCurrentDirectory(_path);
Application path
//数据持久化路径
Application.persistentDataPath
//Assets相对路径
Application.dataPath
//StreamingAssets外部加载路径
Application.StreamingAssets
AssetDatabase path method
//获取 Assets 路径下和 ProjectSettings 路径下所有资源文件(不包括meta文件) AssetDatabase.GetAllAssetPaths() // 获得相对路径 参数为Object AssetDatabase.GetAssetPath(Object) AssetDatabase.GetAssetOrScenePath(SelectObject) // 根据相对路径获取该路径下的所有子文件夹 忽略.meta string[] subFolders = AssetDatabase.GetSubFolders("Assets");
2. Get all files under the specified path
public void GetAllFilesAndDertorys(string _path)
{
//判断路径是否存在
if (Directory.Exists(_path))
{
DirectoryInfo dir = new DirectoryInfo(_path);
//获取目标路径下的所有文件与文件夹
FileSystemInfo[] allFilesAndDir = dir.GetFileSystemInfos("*",SearchOption.AllDirectories);
//获取目标路径下的所有文件
FileInfo[] allFiles = dir.GetFiles("*", SearchOption.AllDirectories);
//获取目标路径下的单层文件
FileInfo[] files = dir.GetFiles("*");
//获取目标路径下的所有文件夹
DirectoryInfo[] allDirs = dir.GetDirectories("*", SearchOption.AllDirectories);
//获取目标路径下的单层文件夹
DirectoryInfo[] dirs = dir.GetDirectories("*");
foreach(var item in files)
{
//忽略.meta
if (item.Name.EndsWith(".meta")) continue;
//返回文件绝对路径 等于item.FullName
Debug.Log(item);
//返回文件带后缀名称
Debug.Log(item.Name);
//返回相对路径
string assetsName = item.FullName;
assetsName = assetsName.Substring(assetsName.IndexOf("Assets"));
Debug.Log(assetsName);
}
}
}