namespace deleteUtility { using System.Collections; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using UnityEditor; using UnityEngine; public class DeleteUtility : MonoBehaviour { #region Config public string Folder; #endregion [MenuItem("Assets/RecycleAll", false, 0)] public static void RecycleAll() { if (!EditorUtility.DisplayDialog("注意", "回收选择的对象?", "确定", "取消")) { return; } for (int i = 0; i < Selection.objects.Length; i++) { string path = AssetDatabase.GetAssetPath(Selection.objects[i]); string metaFilePath = path + ".meta"; bool isDirectory = Directory.Exists(path); if (isDirectory) { List childPathes = new List(); childPathes = Directory.GetFiles(path, "*.*", SearchOption.AllDirectories).ToList(); for (int j = 0; j < childPathes.Count; j++) { BlockObect(childPathes[j]); } } else { BlockObect(path); } DeleteUtility instance = InstanceManager.SearchInstance(); if (isDirectory) { string recyclePath = $"{instance.Folder}{Path.DirectorySeparatorChar}{Path.GetFileName(path)}"; recyclePath = recyclePath.GetUnRepeatDirectoryName(); Directory.Move(path, recyclePath); File.Delete(metaFilePath); Debug.LogWarning($"回收文件夹 {Selection.objects[i].name}"); AssetDatabase.Refresh(); } else { string recyclePath = $"{instance.Folder}{Path.DirectorySeparatorChar}{Path.GetFileName(path)}"; recyclePath = recyclePath.GetUnRepeatFileName(); File.Move(path, recyclePath); File.Delete(metaFilePath); Debug.Log($"回收文件 {Selection.objects[i].name}"); AssetDatabase.Refresh(); } } } private static void BlockObect(string path) { if (path.EndsWith(".cs")) { StringBuilder stringBuilder = new StringBuilder(); StreamReader streamReader = new StreamReader(path); while (!streamReader.EndOfStream) { stringBuilder.AppendLine("//" + streamReader.ReadLine()); } streamReader.Close(); StreamWriter streamWriter = new StreamWriter(path); streamWriter.Write(stringBuilder.ToString()); streamWriter.Close(); } AssetImporter assetImporter = AssetImporter.GetAtPath(path); if (assetImporter != null && assetImporter.assetBundleName != "" && assetImporter.assetBundleVariant != "") { assetImporter.SetAssetBundleNameAndVariant("", ""); assetImporter.SaveAndReimport(); } TextureImporter textureImporter = AssetImporter.GetAtPath(path) as TextureImporter; if (textureImporter != null && textureImporter.spritePackingTag != "") { textureImporter.spritePackingTag = ""; textureImporter.SaveAndReimport(); } } } }