DeleteUtility.cs 3.4 KB

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