DeleteUtility.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. namespace deleteUtility
  2. {
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Text;
  8. using UnityEditor;
  9. using UnityEngine;
  10. public class DeleteUtility : MonoBehaviour
  11. {
  12. #region Config
  13. public string Folder;
  14. #endregion
  15. [MenuItem("Assets/RecycleAll", false, 0)]
  16. public static void RecycleAll()
  17. {
  18. if (!EditorUtility.DisplayDialog("注意", "回收选择的对象?", "确定", "取消"))
  19. {
  20. return;
  21. }
  22. for (int i = 0; i < Selection.objects.Length; i++)
  23. {
  24. string path = AssetDatabase.GetAssetPath(Selection.objects[i]);
  25. string metaFilePath = path + ".meta";
  26. bool isDirectory = Directory.Exists(path);
  27. if (isDirectory)
  28. {
  29. List<string> childPathes = new List<string>();
  30. childPathes = Directory.GetFiles(path, "*.*", SearchOption.AllDirectories).ToList();
  31. for (int j = 0; j < childPathes.Count; j++)
  32. {
  33. BlockObect(childPathes[j]);
  34. }
  35. }
  36. else
  37. {
  38. BlockObect(path);
  39. }
  40. DeleteUtility instance = InstanceManager.SearchInstance<DeleteUtility>();
  41. if (isDirectory)
  42. {
  43. string recyclePath = $"{instance.Folder}{Path.DirectorySeparatorChar}{Path.GetFileName(path)}";
  44. recyclePath = recyclePath.GetUnRepeatDirectoryName();
  45. Directory.Move(path, recyclePath);
  46. File.Delete(metaFilePath);
  47. Debug.LogWarning($"回收文件夹 {Selection.objects[i].name}");
  48. AssetDatabase.Refresh();
  49. }
  50. else
  51. {
  52. string recyclePath = $"{instance.Folder}{Path.DirectorySeparatorChar}{Path.GetFileName(path)}";
  53. recyclePath = recyclePath.GetUnRepeatFileName();
  54. File.Move(path, recyclePath);
  55. File.Delete(metaFilePath);
  56. Debug.Log($"回收文件 {Selection.objects[i].name}");
  57. AssetDatabase.Refresh();
  58. }
  59. }
  60. }
  61. private static void BlockObect(string path)
  62. {
  63. if (path.EndsWith(".cs"))
  64. {
  65. StringBuilder stringBuilder = new StringBuilder();
  66. StreamReader streamReader = new StreamReader(path);
  67. while (!streamReader.EndOfStream)
  68. {
  69. stringBuilder.AppendLine("//" + streamReader.ReadLine());
  70. }
  71. streamReader.Close();
  72. StreamWriter streamWriter = new StreamWriter(path);
  73. streamWriter.Write(stringBuilder.ToString());
  74. streamWriter.Close();
  75. }
  76. AssetImporter assetImporter = AssetImporter.GetAtPath(path);
  77. if (assetImporter != null && assetImporter.assetBundleName != "" && assetImporter.assetBundleVariant != "")
  78. {
  79. assetImporter.SetAssetBundleNameAndVariant("", "");
  80. assetImporter.SaveAndReimport();
  81. }
  82. TextureImporter textureImporter = AssetImporter.GetAtPath(path) as TextureImporter;
  83. if (textureImporter != null && textureImporter.spritePackingTag != "")
  84. {
  85. textureImporter.spritePackingTag = "";
  86. textureImporter.SaveAndReimport();
  87. }
  88. }
  89. }
  90. }