从 Resources 子文件夹中获取文件名
Get file names from Resources sub folder
在我的 Resources 文件夹中有一个图像子文件夹,我想从该文件夹中获取这些图像的所有文件名。
尝试了几种 Resources.loadAll
方法来获取 .name 但没有成功
是实现我在这里尝试做的事情的正确做法吗?
嗯...为什么不试试这个。
using System.IO;
Const String path = ""; /file path
private void GetFiles()
{
string [] files = Directory.GetFiles (path, "*.*");
foreach (string sourceFile in files)
{
string fileName = Path.GetFileName (sourceFile);
Debug.Log("fileName");
}
}
没有内置的 API 可以执行此操作,因为信息不在您构建之后。你甚至不能用 中的内容来做到这一点。这只适用于编辑器。当您构建项目时,您的代码将失败。
这是要做的事情:
1。在 OnPreprocessBuild
函数中检测构建按钮何时被点击或构建即将发生。
2。获取所有带Directory.GetFiles
的文件名,序列化为json保存到Resources文件夹。我们使用 json 来更容易读取单个文件名。您不必使用 json。您必须排除 ".meta" 扩展名。
步骤 1 和 2 在编辑器中完成。
3。在构建之后或在 运行 期间,您可以访问包含文件名的已保存文件作为 TextAsset
和 Resources.Load<TextAsset>("FileNames")
然后从 [= 反序列化 json 16=].
下面是一个非常简单的例子。没有错误处理,这取决于您实施。当您单击构建按钮时,下面的编辑器脚本会保存文件名:
[Serializable]
public class FileNameInfo
{
public string[] fileNames;
public FileNameInfo(string[] fileNames)
{
this.fileNames = fileNames;
}
}
class PreBuildFileNamesSaver : IPreprocessBuildWithReport
{
public int callbackOrder { get { return 0; } }
public void OnPreprocessBuild(UnityEditor.Build.Reporting.BuildReport report)
{
//The Resources folder path
string resourcsPath = Application.dataPath + "/Resources";
//Get file names except the ".meta" extension
string[] fileNames = Directory.GetFiles(resourcsPath)
.Where(x => Path.GetExtension(x) != ".meta").ToArray();
//Convert the Names to Json to make it easier to access when reading it
FileNameInfo fileInfo = new FileNameInfo(fileNames);
string fileInfoJson = JsonUtility.ToJson(fileInfo);
//Save the json to the Resources folder as "FileNames.txt"
File.WriteAllText(Application.dataPath + "/Resources/FileNames.txt", fileInfoJson);
AssetDatabase.Refresh();
}
}
在运行期间,您可以使用以下示例检索保存的文件名:
//Load as TextAsset
TextAsset fileNamesAsset = Resources.Load<TextAsset>("FileNames");
//De-serialize it
FileNameInfo fileInfoLoaded = JsonUtility.FromJson<FileNameInfo>(fileNamesAsset.text);
//Use data?
foreach (string fName in fileInfoLoaded.fileNames)
{
Debug.Log(fName);
}
在我的 Resources 文件夹中有一个图像子文件夹,我想从该文件夹中获取这些图像的所有文件名。
尝试了几种 Resources.loadAll
方法来获取 .name 但没有成功
是实现我在这里尝试做的事情的正确做法吗?
嗯...为什么不试试这个。
using System.IO;
Const String path = ""; /file path
private void GetFiles()
{
string [] files = Directory.GetFiles (path, "*.*");
foreach (string sourceFile in files)
{
string fileName = Path.GetFileName (sourceFile);
Debug.Log("fileName");
}
}
没有内置的 API 可以执行此操作,因为信息不在您构建之后。你甚至不能用
这是要做的事情:
1。在 OnPreprocessBuild
函数中检测构建按钮何时被点击或构建即将发生。
2。获取所有带Directory.GetFiles
的文件名,序列化为json保存到Resources文件夹。我们使用 json 来更容易读取单个文件名。您不必使用 json。您必须排除 ".meta" 扩展名。
步骤 1 和 2 在编辑器中完成。
3。在构建之后或在 运行 期间,您可以访问包含文件名的已保存文件作为 TextAsset
和 Resources.Load<TextAsset>("FileNames")
然后从 [= 反序列化 json 16=].
下面是一个非常简单的例子。没有错误处理,这取决于您实施。当您单击构建按钮时,下面的编辑器脚本会保存文件名:
[Serializable]
public class FileNameInfo
{
public string[] fileNames;
public FileNameInfo(string[] fileNames)
{
this.fileNames = fileNames;
}
}
class PreBuildFileNamesSaver : IPreprocessBuildWithReport
{
public int callbackOrder { get { return 0; } }
public void OnPreprocessBuild(UnityEditor.Build.Reporting.BuildReport report)
{
//The Resources folder path
string resourcsPath = Application.dataPath + "/Resources";
//Get file names except the ".meta" extension
string[] fileNames = Directory.GetFiles(resourcsPath)
.Where(x => Path.GetExtension(x) != ".meta").ToArray();
//Convert the Names to Json to make it easier to access when reading it
FileNameInfo fileInfo = new FileNameInfo(fileNames);
string fileInfoJson = JsonUtility.ToJson(fileInfo);
//Save the json to the Resources folder as "FileNames.txt"
File.WriteAllText(Application.dataPath + "/Resources/FileNames.txt", fileInfoJson);
AssetDatabase.Refresh();
}
}
在运行期间,您可以使用以下示例检索保存的文件名:
//Load as TextAsset
TextAsset fileNamesAsset = Resources.Load<TextAsset>("FileNames");
//De-serialize it
FileNameInfo fileInfoLoaded = JsonUtility.FromJson<FileNameInfo>(fileNamesAsset.text);
//Use data?
foreach (string fName in fileInfoLoaded.fileNames)
{
Debug.Log(fName);
}