using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.IO;
using System.Linq;
using System;
using System.Text;
using System.Reflection;
public class ReverseDependencies
{
[MenuItem("优化工具/反向引用/刷新")]
static void DoLogic()
{
DoSeach(null);
}
[MenuItem("优化工具/反向引用/刷新 From Config")]
static void DoLogicIf()
{
Load();
if (m_dicReverseDeps == null)
{
DoSeach(null);
}
}
static void DoSeach(System.Action onFinish)
{
m_dicReverseDeps = new Dictionary<string, Dictionary<Type, List<string>>>();
List<string> withoutExtensions = new List<string>() { ".prefab", ".unity", ".asset", ".mat", ".png", ".tga", ".jpg" };
//string[] files = Directory.GetFiles(@"E:\copywork\UnityArt\Assets\FBX\Effect\Prefab_UI\AnJianTX", "*.*", SearchOption.AllDirectories)
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);
ParseOne(file);
startIndex++;
if (isCancel || startIndex >= files.Length)
{
EditorUtility.ClearProgressBar();
EditorApplication.update = null;
startIndex = 0;
Debug.Log("检查结束");
Save();
onFinish?.Invoke();
}
};
}
/// <summary>
/// 内容集合, key对象GUID <类型, 引用对象GUID>
/// </summary>
private static Dictionary<string, Dictionary<Type, List<string>>> m_dicReverseDeps = null;
private static void ParseOne(string strPath)
{
strPath = strPath.Replace("\\", "/");
string strLocalPath = strPath.Replace(Application.dataPath, "Assets");
string strKeyGUID = AssetDatabase.AssetPathToGUID(strLocalPath); // 获取对象的GUID, 作为键值对的key
Type typeKey = AssetDatabase.GetMainAssetTypeAtPath(strLocalPath);
string[] arrDeps = AssetDatabase.GetDependencies(strLocalPath); // 获取所有依赖;
for (int i = 0; i < arrDeps.Length; i++)
{
string strGUIDDep = AssetDatabase.AssetPathToGUID(arrDeps[i]);
Dictionary<Type, List<string>> dic;
if (!m_dicReverseDeps.TryGetValue(strGUIDDep, out dic))
{
dic = new Dictionary<Type, List<string>>();
m_dicReverseDeps.Add(strGUIDDep, dic);
}
List<string> list;
if (!dic.TryGetValue(typeKey, out list))
{
list = new List<string>();
dic.Add(typeKey, list);
}
list.Add(strKeyGUID);
}
}
private static void Load()
{
string strPath = Path.Combine(Application.dataPath, "..", "reverse_dependencies.txt");
if (!File.Exists(strPath))
{
return;
}
m_dicReverseDeps = new Dictionary<string, Dictionary<Type, List<string>>>();
string strData = File.ReadAllText(strPath, Encoding.UTF8);
string[] arrLines = strData.Split(new string[] { "\n"}, StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; i < arrLines.Length; i++)
{
var kv = arrLines[i].Split(new string[] { "{k}" }, StringSplitOptions.RemoveEmptyEntries);
string strKey = kv[0];
m_dicReverseDeps.Add(strKey, new Dictionary<Type, List<string>>());
string[] arrParts = kv[1].Split(new string[] { "{p}" }, StringSplitOptions.RemoveEmptyEntries);
for (int j = 0; j < arrParts.Length; j++)
{
string[]arrt2l = arrParts[j].Split(new string[] { "{t}" }, StringSplitOptions.RemoveEmptyEntries);
string[] arrtype = arrt2l[0].Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
string[] arrlist = arrt2l[1].Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
Assembly asmb = Assembly.LoadFrom(arrtype[0]);
Type type = asmb.GetType(arrtype[1]);
m_dicReverseDeps[strKey].Add(type, new List<string>(arrlist));
}
}
}
private static void Save()
{
StringBuilder strBuilder = new StringBuilder();
foreach (var item in m_dicReverseDeps)
{
strBuilder.Append(item.Key);
strBuilder.Append("{k}");
Dictionary<Type, List<string>> dic = item.Value;
foreach (var tl in dic)
{
strBuilder.Append(tl.Key.Module.FullyQualifiedName);
strBuilder.Append(",");
strBuilder.Append(tl.Key.FullName);
strBuilder.Append("{t}");
List<string> list = tl.Value;
for (int i = 0; i < list.Count; i++)
{
strBuilder.Append(list[i]);
strBuilder.Append(",");
}
strBuilder.Append("{p}");
}
strBuilder.Append("\n");
}
string strPath = Path.Combine(Application.dataPath, "..", "reverse_dependencies.txt");
File.WriteAllText(strPath, strBuilder.ToString(), Encoding.UTF8);
}
//----以下 对外 查询接口
public static List<string> GetReverseDeps(string strGUID, Type type)
{
if (m_dicReverseDeps == null)
{
return null;
}
Dictionary<Type, List<string>> dic;
if (!m_dicReverseDeps.TryGetValue(strGUID, out dic))
{
return null;
}
List<string> list;
if (!dic.TryGetValue(type, out list))
{
return null;
}
return list;
}
}
程序学无止尽。 欢迎大家沟通,有啥不明确的,或者不对的,也可以和我私聊 我的QQ 334524067 神一般的狄狄
|