unity遍历文件夹找图图片
时间: 2023-09-12 15:10:49 浏览: 113
你可以使用Unity的AssetDatabase来遍历文件夹并找到图片。以下是一个示例代码:
```
using UnityEngine;
using UnityEditor;
using System.IO;
public class ImageFinder : MonoBehaviour
{
[SerializeField] private string folderPath = "Assets/Images";
private void Start()
{
DirectoryInfo dir = new DirectoryInfo(folderPath);
FileInfo[] info = dir.GetFiles("*.*");
foreach (FileInfo f in info)
{
if (f.Extension.ToLower() == ".png" || f.Extension.ToLower() == ".jpg" || f.Extension.ToLower() == ".jpeg")
{
string filePath = "Assets/" + folderPath + "/" + f.Name;
Sprite sprite = AssetDatabase.LoadAssetAtPath<Sprite>(filePath);
if (sprite != null)
{
// Do something with the sprite
Debug.Log("Found sprite: " + sprite.name);
}
}
}
}
}
```
这个代码会在指定的文件夹中查找所有的PNG、JPG和JPEG图片文件,并将它们加载成Sprite对象。你可以在if语句中添加其他文件类型的判断条件。注意,该代码必须在Unity编辑器中运行,因为它使用了Unity的AssetDatabase API。
阅读全文
相关推荐


















