Unity: Get Files in Folder Guide
In Unity, you can use the Directory class in the System.IO namespace to get all the files in a specified folder. Here is an example code:
using System.IO;
public class FileLoader : MonoBehaviour
{
void Start()
{
string folderPath = "Assets/Resources"; // 要获取文件的文件夹路径
string[] files = Directory.GetFiles(folderPath); // 获取指定文件夹中的所有文件
foreach (string file in files)
{
Debug.Log("File found: " + file);
}
}
}
The code above first specifies the folder path of the files to be retrieved, then uses the Directory.GetFiles method to retrieve all the files in that folder and store the results in a string array. Finally, loop through the array to print out the path of each file. Remember to attach this script to a GameObject in the Unity scene to execute it.