From 837ba149dd459ddf0a2da21447a3a012c0352a62 Mon Sep 17 00:00:00 2001 From: StarBeats <977663818@qq.com> Date: Thu, 23 Oct 2025 19:10:35 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=9C=A8project=E4=B8=AD?= =?UTF-8?q?=E6=98=BE=E7=A4=BA=E5=BC=95=E7=94=A8=E6=AC=A1=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../AssetDependencyGraph-Editor.asmdef | 4 +- .../Editor/AssetDependencyGraph.cs | 32 +++++++++- .../Editor/AssetRefCountView.cs | 60 +++++++++++++++++++ .../Editor/AssetRefCountView.cs.meta | 11 ++++ UnityDependencyAnalyzer/Program.cs | 5 +- .../PublishProfiles/FolderProfile.pubxml.user | 2 +- 6 files changed, 106 insertions(+), 8 deletions(-) create mode 100644 AssetDependencyGraph/Editor/AssetRefCountView.cs create mode 100644 AssetDependencyGraph/Editor/AssetRefCountView.cs.meta diff --git a/AssetDependencyGraph/AssetDependencyGraph-Editor.asmdef b/AssetDependencyGraph/AssetDependencyGraph-Editor.asmdef index e107d52..9542ffb 100644 --- a/AssetDependencyGraph/AssetDependencyGraph-Editor.asmdef +++ b/AssetDependencyGraph/AssetDependencyGraph-Editor.asmdef @@ -1,7 +1,9 @@ { "name": "AssetDependencyGraph-Editor", "rootNamespace": "", - "references": [], + "references": [ + "GUID:55f6a074926ddfb4bb5d7061e6611a6f" + ], "includePlatforms": [ "Editor" ], diff --git a/AssetDependencyGraph/Editor/AssetDependencyGraph.cs b/AssetDependencyGraph/Editor/AssetDependencyGraph.cs index 1e69f9f..c25343a 100644 --- a/AssetDependencyGraph/Editor/AssetDependencyGraph.cs +++ b/AssetDependencyGraph/Editor/AssetDependencyGraph.cs @@ -2,6 +2,7 @@ using MemoryPack; using System; using System.Collections.Generic; using System.IO; +using System.Linq; using UnityEditor; using UnityEditor.Experimental.GraphView; using UnityEditor.UIElements; @@ -137,6 +138,27 @@ namespace AssetDependencyGraph 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() { System.Diagnostics.Stopwatch stopwatch = new(); @@ -249,8 +271,14 @@ namespace AssetDependencyGraph { text = "分析或更新引用" }); - - var ts = new ToolbarSearchField(); + toolbar.Add(new Button(() => + { + AssetRefCountView.HookAssetViewer(GetRefCountByGUID); + }) + { + text = "在 Project 面板显示被引用", + }); + var ts = new ToolbarSearchField(); ts.RegisterValueChangedCallback(x => { if (string.IsNullOrEmpty(x.newValue)) diff --git a/AssetDependencyGraph/Editor/AssetRefCountView.cs b/AssetDependencyGraph/Editor/AssetRefCountView.cs new file mode 100644 index 0000000..3c3939a --- /dev/null +++ b/AssetDependencyGraph/Editor/AssetRefCountView.cs @@ -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 getRefCountByGuid; + + internal static void HookAssetViewer(Func 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"); + } + + } +} diff --git a/AssetDependencyGraph/Editor/AssetRefCountView.cs.meta b/AssetDependencyGraph/Editor/AssetRefCountView.cs.meta new file mode 100644 index 0000000..cea7c7c --- /dev/null +++ b/AssetDependencyGraph/Editor/AssetRefCountView.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: c7596c1e3d21d0f4196c4685d503b763 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/UnityDependencyAnalyzer/Program.cs b/UnityDependencyAnalyzer/Program.cs index 4e1b113..b907095 100644 --- a/UnityDependencyAnalyzer/Program.cs +++ b/UnityDependencyAnalyzer/Program.cs @@ -1,8 +1,5 @@ -using System.Runtime.InteropServices; +using AssetDependencyGraph; using System.Text.Json; -using AssetDependencyGraph; -using UnityFileApi; -UnityFileSystem.Init(); switch (Environment.GetCommandLineArgs()[1]) { diff --git a/UnityDependencyAnalyzer/Properties/PublishProfiles/FolderProfile.pubxml.user b/UnityDependencyAnalyzer/Properties/PublishProfiles/FolderProfile.pubxml.user index 018fb50..b86eb26 100644 --- a/UnityDependencyAnalyzer/Properties/PublishProfiles/FolderProfile.pubxml.user +++ b/UnityDependencyAnalyzer/Properties/PublishProfiles/FolderProfile.pubxml.user @@ -4,7 +4,7 @@ https://go.microsoft.com/fwlink/?LinkID=208121. --> - 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||; + 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||; \ No newline at end of file