miduo_client/Assets/Scripts/Editor/GameEditor/FrameTool/DeleteResWindow.cs

282 lines
11 KiB
C#

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 + "/deleteFile.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 (dependDic.ContainsKey(resPath))
{
for (int j = 0; j < dependDic[resPath].Count; j++)
{
if (!dependUnUsedDic.ContainsKey(dependDic[resPath][j]))
{
dependUnUsedDic[dependDic[resPath][j]] = new List<string>();
}
dependUnUsedDic[dependDic[resPath][j]].Add(dependDic[resPath][j]);
}
}
}
}
//foreach (KeyValuePair<string, List<string>> keyValue in dependUnUsedDic)
//{
// Debug.Log("-------------------------------------------------------");
// for (int i = 0; i < keyValue.Value.Count; i++)
// {
// Debug.Log("keyValue.Value[i]:" + keyValue.Value[i]);
// }
//}
}
static void FindUnUsedDepend()
{
int num = 0;
foreach (KeyValuePair<string, List<string>> keyValue in dependUnUsedDic)
{
num = num + 1;
EditorUtility.DisplayProgressBar("寻找依赖资源", resName, num*1.0f/ dependUnUsedDic.Count);
for (int i = 0; i < keyValue.Value.Count;i++ )
{
FindUnUsedDependByPath(keyValue.Value[i]);
}
}
//foreach (KeyValuePair<string, List<string>> keyValue in dic)
//{
// Debug.Log("-------------------------------------------------------");
// for (int i = 0; i < keyValue.Value.Count; i++)
// {
// Debug.Log("keyValue.Value[i]:" + keyValue.Value[i]);
// }
//}
}
static void FindUnUsedDependByPath(string resPath)
{
//Debug.Log("-------------------------------------------------------");
if (refrenceDic.ContainsKey(resPath))
{
bool isDelete = true;
for (int i = 0; i < refrenceDic[resPath].Count; i++)
{
//Debug.Log(" refrenceDic[resPath]:"+ refrenceDic[resPath][i]);
if (!dependUnUsedDic.ContainsKey(refrenceDic[resPath][i]))
{
isDelete = false;
break;
}
}
//Debug.Log("isDelete:" + isDelete);
if (!dic.ContainsKey(resPath))
dic[resPath] = new List<string>();
if (isDelete)
{
dic[resPath].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('\\', '/');
}
}
}