代码暂记,记事板
using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
public class ImageGeneratePrefab : MonoBehaviour
{
private static string AssetsPath = Environment.CurrentDirectory + "/Assets";
private static string PrefabsPath = AssetsPath + "/Art/Prefabs";
[MenuItem("Tools/GeneratePrefabs")]
static void GeneratePrefabs()
{
DeletePrefabs();
if (!Directory.Exists(PrefabsPath))
{
Debug.LogError("Art/Prefabs目录不存在!请检查");
Directory.CreateDirectory(PrefabsPath);
}
//生成对应文件夹名字
string SpritesPath = "Assets/Art/Sprites";
List<string> allFolder = new List<string>();
string[] subFolderGUIDs = AssetDatabase.FindAssets("t:Folder", new[] { SpritesPath });
foreach (string guid in subFolderGUIDs)
{
string subFolderPath = AssetDatabase.GUIDToAssetPath(guid);
if (IsDirectSubfolder(SpritesPath, subFolderPath))
{
string subFolderName = Path.GetFileName(subFolderPath);
allFolder.Add(subFolderName);
Directory.CreateDirectory(PrefabsPath + "/" + subFolderName);
}
}
//可以考虑在allFolder内检查文件系统中是否有违规文件,但从时间和性能考虑不一定推荐
foreach (string subFolderName in allFolder)
{
}
/*
var files = new List<string>();
GetAllUnityFilesInFolder(AssetDatabase.GUIDToAssetPath(AssetDatabase.FindAssets("t:File", new[] { SpritesPath })[0]), files);
foreach (var file in files)
{
Debug.Log(file);
}*/
//刷新unity所有资源,有性能消耗,如果性能消耗过大应用AssetDatabase.ImportAsset代替
AssetDatabase.Refresh();
}
// 检查路径是否为另一个路径的直接子文件夹
private static bool IsDirectSubfolder(string parentPath, string childPath)
{
// 获取父路径的最后一部分,即父文件夹名
string parentFolderName = Path.GetFileName(parentPath);
// 获取子路径的父文件夹路径的最后一部分
string childParentPath = Path.GetFileName(Directory.GetParent(childPath).FullName);
// 检查子路径的父文件夹是否是指定的父文件夹
return childParentPath != null && childParentPath == parentFolderName;
}
private static void GetAllUnityFilesInFolder(string folderGUID, List<string> files)
{
string[] subAssets = AssetDatabase.FindAssets("t:File", new[] { folderGUID });
foreach (string subAsset in subAssets)
{
string assetPath = AssetDatabase.GUIDToAssetPath(subAsset);
// 检查是否为文件而不是文件夹
if (!Directory.Exists(assetPath))
{
files.Add(Path.GetFileName(assetPath));
}
else
{
// 递归搜索子文件夹
GetAllUnityFilesInFolder(subAsset, files);
}
}
}
[MenuItem("Tools/DeletePrefabs")]
static void DeletePrefabs()
{
if (!Directory.Exists(PrefabsPath))
{
Debug.LogError("Art/Prefabs目录不存在!请检查");
return;
}
string[] allFiles = Directory.GetFiles(PrefabsPath);
string[] allSubDirectories = Directory.GetDirectories(PrefabsPath);
foreach (string file in allFiles)
{
File.Delete(file);
}
foreach (string subDirectory in allSubDirectories)
{
DeleteFolder(subDirectory);
}
Debug.Log("Assets/Prefabs清理完成");
}
private static void DeleteFolder(string folderPath)
{
if (!Directory.Exists(folderPath))
{
Debug.Log("文件夹不存在,请检查");
return;
}
try
{
string[] allFiles = Directory.GetFiles(folderPath);
string[] allSubDirectories = Directory.GetDirectories(folderPath);
foreach (string file in allFiles)
{
File.Delete(file);
}
foreach (string subDirectory in allSubDirectories)
{
DeleteFolder(subDirectory);
}
Directory.Delete(folderPath, false);
}
catch (Exception ex)
{
Debug.LogError(ex.Message);
}
}
}