123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- 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<string> childPathes = new List<string>();
- 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<DeleteUtility>();
- 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();
- }
- }
- }
- }
|