Unity 获取文件夹下所有资源文件 - 代码示例与解析
可以使用 Unity 的 AssetDatabase 类来获取文件夹下的所有资源文件。
首先,需要使用 AssetDatabase 类的 GetAllAssetPaths 方法获取所有的资源路径,然后使用 Path 类的 GetDirectoryName 方法获取文件夹路径,最后使用 Directory 类的 GetFiles 方法获取文件夹下的所有文件。
以下是示例代码:
using UnityEngine;
using UnityEditor;
using System.IO;
public class Example : MonoBehaviour
{
[MenuItem("Example/Get All Assets")]
static void GetAllAssets()
{
string[] assetPaths = AssetDatabase.GetAllAssetPaths();
foreach (string path in assetPaths)
{
if (Directory.Exists(path))
{
string[] files = Directory.GetFiles(path);
foreach (string file in files)
{
Debug.Log(file);
}
}
}
}
}
这样就可以获取文件夹下的所有资源文件了。需要注意的是,AssetDatabase.GetAllAssetPaths 方法返回的路径包括文件和文件夹,需要判断是否是文件夹再进行操作。
原文地址: https://www.cveoy.top/t/topic/nVwj 著作权归作者所有。请勿转载和采集!