增加在project中显示引用次数

This commit is contained in:
StarBeats 2025-10-23 19:10:35 +08:00
parent f6d3bf65bc
commit 837ba149dd
6 changed files with 106 additions and 8 deletions

View File

@ -1,7 +1,9 @@
{ {
"name": "AssetDependencyGraph-Editor", "name": "AssetDependencyGraph-Editor",
"rootNamespace": "", "rootNamespace": "",
"references": [], "references": [
"GUID:55f6a074926ddfb4bb5d7061e6611a6f"
],
"includePlatforms": [ "includePlatforms": [
"Editor" "Editor"
], ],

View File

@ -2,6 +2,7 @@ using MemoryPack;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq;
using UnityEditor; using UnityEditor;
using UnityEditor.Experimental.GraphView; using UnityEditor.Experimental.GraphView;
using UnityEditor.UIElements; using UnityEditor.UIElements;
@ -137,6 +138,27 @@ namespace AssetDependencyGraph
return AssetDatabase.AssetPathToGUID(path); return AssetDatabase.AssetPathToGUID(path);
} }
private static int GetRefCountByGUID(string guid)
{
var path = GUIDToPath(guid);
if (string.IsNullOrEmpty(path))
{
return 0;
}
if(path2NodeIdDic == null || assetId2NodeDic == null)
{
return 0;
}
if(path2NodeIdDic.TryGetValue(path, out var id) && assetId2NodeDic.TryGetValue(id, out var assetNode))
{
return assetNode.DependentSet.Count(n => n.AssetType != "Folder");
}
return 0;
}
void ResolveDependency() void ResolveDependency()
{ {
System.Diagnostics.Stopwatch stopwatch = new(); System.Diagnostics.Stopwatch stopwatch = new();
@ -249,8 +271,14 @@ namespace AssetDependencyGraph
{ {
text = "分析或更新引用" text = "分析或更新引用"
}); });
toolbar.Add(new Button(() =>
var ts = new ToolbarSearchField(); {
AssetRefCountView.HookAssetViewer(GetRefCountByGUID);
})
{
text = "在 Project 面板显示被引用",
});
var ts = new ToolbarSearchField();
ts.RegisterValueChangedCallback(x => ts.RegisterValueChangedCallback(x =>
{ {
if (string.IsNullOrEmpty(x.newValue)) if (string.IsNullOrEmpty(x.newValue))

View File

@ -0,0 +1,60 @@
using MonoHook;
using System;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using UnityEditor;
using UnityEngine;
namespace AssetDependencyGraph
{
internal class AssetRefCountView
{
private static MethodHook hook;
private static FieldInfo assetNameFiled;
private static Func<string, int> getRefCountByGuid;
internal static void HookAssetViewer(Func<string, int> getRefCountByGuid)
{
AssetRefCountView.getRefCountByGuid = getRefCountByGuid;
var editorCore = System.AppDomain.CurrentDomain.GetAssemblies().Where(a => a.GetName().Name.Equals("UnityEditor.CoreModule")).FirstOrDefault();
var targetMethod = editorCore.GetType("UnityEditor.FilteredHierarchy").GetMethod("CopyPropertyData", BindingFlags.Instance | BindingFlags.NonPublic);
assetNameFiled = editorCore.GetType("UnityEditor.FilteredHierarchy+FilterResult").GetField("name", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
hook = new MethodHook(targetMethod, typeof(AssetRefCountView).GetMethod("FuncReplace", BindingFlags.Instance | BindingFlags.NonPublic), typeof(AssetRefCountView).GetMethod("FuncProxy", BindingFlags.Instance | BindingFlags.NonPublic));
hook.Install();
}
internal static void UnHookAssetViewer()
{
if (hook != null)
{
hook.Uninstall();
}
hook = null;
}
[MethodImpl(MethodImplOptions.NoOptimization)]
private void FuncReplace(ref object result, HierarchyProperty property)
{
FuncProxy(ref result, property);
if (!property.isFolder)
{
var refCnt = getRefCountByGuid.Invoke(property.guid);
if(refCnt != 0)
{
assetNameFiled.SetValue(result, $"{assetNameFiled.GetValue(result)} [被引用次数:{refCnt}]");
}
}
}
[MethodImpl(MethodImplOptions.NoOptimization)]
private void FuncProxy(ref object result, HierarchyProperty property)
{
Debug.Log("Not need code");
}
}
}

View File

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

View File

@ -1,8 +1,5 @@
using System.Runtime.InteropServices; using AssetDependencyGraph;
using System.Text.Json; using System.Text.Json;
using AssetDependencyGraph;
using UnityFileApi;
UnityFileSystem.Init();
switch (Environment.GetCommandLineArgs()[1]) switch (Environment.GetCommandLineArgs()[1])
{ {

View File

@ -4,7 +4,7 @@ https://go.microsoft.com/fwlink/?LinkID=208121.
--> -->
<Project> <Project>
<PropertyGroup> <PropertyGroup>
<History>True|2025-10-22T12:29:32.7349288Z||;True|2025-07-08T11:14:03.4969097+08:00||;True|2025-07-07T16:09:23.5193447+08:00||;True|2025-07-07T16:07:31.9772710+08:00||;True|2025-07-07T16:07:24.6544267+08:00||;True|2025-07-07T16:06:58.2999030+08:00||;True|2025-07-07T16:06:47.5176281+08:00||;True|2025-07-07T16:06:30.9937062+08:00||;True|2025-07-07T16:06:16.1827410+08:00||;True|2025-07-04T20:51:36.2342905+08:00||;True|2025-07-04T20:49:41.4585526+08:00||;True|2025-07-04T20:46:06.1627373+08:00||;True|2025-07-04T20:39:45.9777563+08:00||;True|2025-07-04T20:38:51.7922210+08:00||;True|2025-07-04T20:38:30.7653415+08:00||;True|2025-07-04T20:37:48.1335374+08:00||;True|2025-07-04T20:37:32.2007568+08:00||;True|2025-07-04T20:37:03.6443318+08:00||;True|2025-07-04T20:35:04.7675245+08:00||;True|2025-04-16T20:37:48.8637838+08:00||;True|2025-04-16T20:23:11.1448355+08:00||;True|2025-04-16T16:49:35.4476343+08:00||;True|2025-04-16T16:36:22.8513231+08:00||;True|2025-04-16T12:23:59.6108463+08:00||;True|2025-04-16T12:08:50.9715020+08:00||;True|2025-04-16T11:57:10.7843966+08:00||;False|2025-04-16T11:02:10.7479206+08:00||;False|2025-04-16T10:59:42.9239923+08:00||;False|2025-04-16T10:54:01.4031170+08:00||;True|2025-04-16T10:29:13.7528358+08:00||;False|2025-04-16T10:27:03.3522077+08:00||;False|2025-04-16T10:26:07.1734998+08:00||;</History> <History>True|2025-10-23T04:49:21.3132987Z||;True|2025-10-22T20:29:32.7349288+08:00||;True|2025-07-08T11:14:03.4969097+08:00||;True|2025-07-07T16:09:23.5193447+08:00||;True|2025-07-07T16:07:31.9772710+08:00||;True|2025-07-07T16:07:24.6544267+08:00||;True|2025-07-07T16:06:58.2999030+08:00||;True|2025-07-07T16:06:47.5176281+08:00||;True|2025-07-07T16:06:30.9937062+08:00||;True|2025-07-07T16:06:16.1827410+08:00||;True|2025-07-04T20:51:36.2342905+08:00||;True|2025-07-04T20:49:41.4585526+08:00||;True|2025-07-04T20:46:06.1627373+08:00||;True|2025-07-04T20:39:45.9777563+08:00||;True|2025-07-04T20:38:51.7922210+08:00||;True|2025-07-04T20:38:30.7653415+08:00||;True|2025-07-04T20:37:48.1335374+08:00||;True|2025-07-04T20:37:32.2007568+08:00||;True|2025-07-04T20:37:03.6443318+08:00||;True|2025-07-04T20:35:04.7675245+08:00||;True|2025-04-16T20:37:48.8637838+08:00||;True|2025-04-16T20:23:11.1448355+08:00||;True|2025-04-16T16:49:35.4476343+08:00||;True|2025-04-16T16:36:22.8513231+08:00||;True|2025-04-16T12:23:59.6108463+08:00||;True|2025-04-16T12:08:50.9715020+08:00||;True|2025-04-16T11:57:10.7843966+08:00||;False|2025-04-16T11:02:10.7479206+08:00||;False|2025-04-16T10:59:42.9239923+08:00||;False|2025-04-16T10:54:01.4031170+08:00||;True|2025-04-16T10:29:13.7528358+08:00||;False|2025-04-16T10:27:03.3522077+08:00||;False|2025-04-16T10:26:07.1734998+08:00||;</History>
<LastFailureDetails /> <LastFailureDetails />
</PropertyGroup> </PropertyGroup>
</Project> </Project>