删除资源工具提交

dev_chengFeng
jiaoyangna 2021-07-28 17:00:14 +08:00
parent 93a9473184
commit b98fd71d35
15 changed files with 274 additions and 118 deletions

View File

@ -1,8 +0,0 @@
fileFormatVersion: 2
guid: fd52b845f4615b2418f482acbcb2d5c6
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -1,8 +0,0 @@
fileFormatVersion: 2
guid: 9aff0d9872fc9e1469693125c1f2c862
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -1,8 +0,0 @@
fileFormatVersion: 2
guid: efc97f44d1121b04a82c298cc2f2bd3a
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -1,10 +0,0 @@
fileFormatVersion: 2
guid: b0f7e0d4b2c88bb48b0eca4a78194329
folderAsset: yes
timeCreated: 1552113377
licenseType: Pro
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -1,10 +0,0 @@
fileFormatVersion: 2
guid: 85993f79eed59024d9e637fd94c9f881
folderAsset: yes
timeCreated: 1553745853
licenseType: Pro
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -1,10 +0,0 @@
fileFormatVersion: 2
guid: ecfc6395934e4694b87e6b3ac204aef6
folderAsset: yes
timeCreated: 1553745942
licenseType: Pro
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -1,10 +0,0 @@
fileFormatVersion: 2
guid: efacd716265078149b4e1bb1298a2b6f
folderAsset: yes
timeCreated: 1553746060
licenseType: Pro
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -1,10 +0,0 @@
fileFormatVersion: 2
guid: dfd5fb09b0968174ca1b9d2dc08a34ba
folderAsset: yes
timeCreated: 1552113391
licenseType: Pro
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -1,10 +0,0 @@
fileFormatVersion: 2
guid: dd8f0b09384a0ae44ac0c9192876c812
folderAsset: yes
timeCreated: 1560330541
licenseType: Pro
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -1,8 +0,0 @@
fileFormatVersion: 2
guid: 691f605092cae4348b2df83b2299845a
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -1,10 +0,0 @@
fileFormatVersion: 2
guid: f943e679a5ef49a4990da9059990b18e
folderAsset: yes
timeCreated: 1555905791
licenseType: Pro
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -1,8 +0,0 @@
fileFormatVersion: 2
guid: 9f6680e5797aa3a4da004c60b970a6d3
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -1,8 +0,0 @@
fileFormatVersion: 2
guid: 84e5263cf14c3fd42b34d74cee22462e
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,263 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using UnityEditor;
using UnityEngine;
using Object = UnityEngine.Object;
namespace GameEditor.Util
{
public class DeleteResWindow : EditorWindow
{
static string tag = Environment.CurrentDirectory;
static string tag1 = "Assets/ManagedResources";
static Dictionary<string, List<string>> dic;
static Dictionary<string, List<string>> dependDic;
static Dictionary<string, List<string>> refrenceDic;
static Dictionary<string, List<string>> dependUnUsedDic;
List<string> upLoadPathList;
static string resName = "";
[MenuItem("资源导入/资源删除")]
static void InitWindow()
{
dic = new Dictionary<string, List<string>>();
dependDic = new Dictionary<string, List<string>>();
refrenceDic = new Dictionary<string, List<string>>();
dependUnUsedDic = new Dictionary<string, List<string>>();
EditorUtility.DisplayProgressBar("建立资源依赖引用关系", "", 0);
SetDependsRefrencesTable();
EditorUtility.DisplayProgressBar("读表", "", 0);
InitData();
EditorUtility.DisplayProgressBar("寻找资源", "", 0);
FindRes();
EditorUtility.DisplayProgressBar("寻找依赖资源", "", 0);
FindUnUsedDepend();
EditorUtility.DisplayProgressBar("删除资源", "", 0);
DeleteRes();
EditorUtility.ClearProgressBar();
}
static void InitData()
{
FileStream fs = new FileStream(tag + "/deleteFile1.csv", FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
StreamReader sr = new StreamReader(fs, System.Text.Encoding.UTF8);
string str = "";
int index = 0;
while (str != null)
{
str = sr.ReadLine();
index = index + 1;
try
{
string[] xu = str.Split(',');
for (int i = 0; i < xu.Length; i++)
{
if (!dic.ContainsKey(xu[i]))
dic[xu[i]] = new List<string>();
}
}
catch
{
Debug.Log(index + "行出错了");
}
}
sr.Close();
fs.Close();
}
static void FindRes()
{
int index = 0;
foreach (KeyValuePair<string, List<string>> keyValue in dic)
{
index = index + 1;
string[] guids = AssetDatabase.FindAssets(keyValue.Key, new string[] { tag1 });
EditorUtility.DisplayProgressBar("寻找资源", keyValue.Key, index * 1.0f / dic.Count);
for (int i = 0; i < guids.Length; i++)
{
string resPath = AssetDatabase.GUIDToAssetPath(guids[i]);
string fileName = Path.GetFileNameWithoutExtension(resPath);
string ext = Path.GetExtension(resPath).ToLower();
if (keyValue.Key != fileName)
{
continue;
}
dic[keyValue.Key].Add(resPath);
if (ext.Contains("tga") || ext.Contains("jpg") || ext.Contains("png") || ext.Contains("mp3") || ext.Contains("wav"))
{ }
else
{
if(dependDic.ContainsKey(resPath))
{
if (!dependUnUsedDic.ContainsKey(keyValue.Key))
{
dependUnUsedDic[keyValue.Key] = new List<string>();
}
dependUnUsedDic[keyValue.Key].AddRange(dependDic[resPath]);
}
}
}
}
}
static void FindUnUsedDepend()
{
int num = 0;
foreach (KeyValuePair<string, List<string>> keyValue in dependUnUsedDic)
{
resName = keyValue.Key;
num = num + 1;
EditorUtility.DisplayProgressBar("寻找依赖资源", resName, num*1.0f/ dependUnUsedDic.Count);
for (int i = 0; i < keyValue.Value.Count;i++ )
{
FindUnUsedDependByPath(keyValue.Value[i]);
}
}
}
static void FindUnUsedDependByPath(string resPath)
{
if (refrenceDic.ContainsKey(resPath))
{
bool isDelete = true;
for (int i = 0; i < refrenceDic[resPath].Count; i++)
{
if (!dependUnUsedDic.ContainsKey(refrenceDic[resPath][i]))
{
isDelete = false;
break;
}
}
if (isDelete)
{
dic[resName].Add(resPath);
}
}
}
static void SetDependsRefrencesTable()
{
string[] guids = AssetDatabase.FindAssets("t:Prefab", new string[] { tag1 });
for (int i = 0; i < guids.Length; i++)
{
string resPath = AssetDatabase.GUIDToAssetPath(guids[i]);
EditorUtility.DisplayProgressBar("建立资源依赖关系", resPath, i * 1.0f / guids.Length);
SetDependsTable(resPath);
}
int num = 0;
foreach (KeyValuePair<string, List<string>> keyValue in dependDic)
{
num = num + 1;
EditorUtility.DisplayProgressBar("建立资源引用关系", keyValue.Key, num * 1.0f / dependDic.Count);
if (keyValue.Value.Count > 0)
{
for (int i = 0; i < keyValue.Value.Count; i++)
{
if (!refrenceDic.ContainsKey(keyValue.Value[i]))
{
refrenceDic[keyValue.Value[i]] = new List<string>();
}
refrenceDic[keyValue.Value[i]].Add(keyValue.Key);
}
}
}
}
static void SetDependsTable(string resPath)
{
if (!dependDic.ContainsKey(resPath))
{
dependDic[resPath] = new List<string>();
string[] files = AssetDatabase.GetDependencies(resPath);
for (int j = 0; j < files.Length; j++)
{
dependDic[resPath].Add(files[j]);
if (!dependDic.ContainsKey(files[j]))
{
SetDependsTable(files[j]);
}
}
}
}
static void DeleteRes()
{
int index = 0;
foreach (KeyValuePair<string, List<string>> keyValue in dic)
{
index = index + 1;
EditorUtility.DisplayProgressBar("删除资源", keyValue.Key, index*1.0f/ dic.Count);
for (int i = 0; i < keyValue.Value.Count; i++)
{
string ext = Path.GetExtension(keyValue.Value[i]).ToLower();
//if (ext.Contains("tga") || ext.Contains("jpg") || ext.Contains("png") || ext.Contains("mp3") || ext.Contains("wav"))
//{
// if (File.Exists(keyValue.Value[i]))
// {
// File.Delete(keyValue.Value[i]);
// }
//}
if (File.Exists(keyValue.Value[i]))
{
File.Delete(keyValue.Value[i]);
}
}
}
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
}
static private void FindReferences()
{
Debug.Log("匹配开始");
EditorSettings.serializationMode = SerializationMode.ForceText;
string path = AssetDatabase.GetAssetPath(Selection.activeObject);
if (!string.IsNullOrEmpty(path))
{
string guid = AssetDatabase.AssetPathToGUID(path);
var withoutExtensions = new List<string>() { ".prefab", ".unity", ".mat", ".asset" };
string[] files = Directory.GetFiles(Application.dataPath, "*.*", SearchOption.AllDirectories)
.Where(s => withoutExtensions.Contains(Path.GetExtension(s).ToLower())).ToArray();
int startIndex = 0;
EditorApplication.update = delegate ()
{
string file = files[startIndex];
bool isCancel = EditorUtility.DisplayCancelableProgressBar("匹配资源中", file, (float)startIndex / (float)files.Length);
if (Regex.IsMatch(File.ReadAllText(file), guid))
{
Debug.Log(file, AssetDatabase.LoadAssetAtPath<Object>(GetRelativeAssetsPath(file)));
}
startIndex++;
if (isCancel || startIndex >= files.Length)
{
EditorUtility.ClearProgressBar();
EditorApplication.update = null;
startIndex = 0;
Debug.Log("匹配结束");
}
};
}
}
static private void FindDependencies()
{
Debug.Log("查找开始");
string path = AssetDatabase.GetAssetPath(Selection.activeObject);
string[] files = AssetDatabase.GetDependencies(path);
for (int i = 0; i < files.Length; i++)
{
if (files[i].Equals(path))
{
continue; //排除自身
}
Debug.Log(files[i], AssetDatabase.LoadAssetAtPath<Object>(GetRelativeAssetsPath(files[i])));
}
Debug.Log("查找结束");
}
static private string GetRelativeAssetsPath(string path)
{
return "Assets" + Path.GetFullPath(path).Replace(Path.GetFullPath(Application.dataPath), "").Replace('\\', '/');
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 7c33f102db514074db7806e4b4e44e88
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: