DeleteUtility.cs 3.3 KB

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