unity editor 查找“被引用”的asset

https://github.com/networm/FindReferencesInProject/blob/master/FindReferencesInProject.cs

 

打印出被谁依赖:

 1 using System.Collections;
 2 using System.Collections.Generic;
 3 using UnityEngine;
 4 using UnityEditor;
 5 
 6 public class FindReferencesInProject
 7 {
 8     private const string MenuItemText = "Assets/Find References In Project";
 9 
10     [MenuItem(MenuItemText, false, 25)]
11     public static void Find()
12     {
13         var sw = new System.Diagnostics.Stopwatch();
14         sw.Start();
15 
16         var referenceCache = new Dictionary<string, List<string>>();
17 
18         string[] guids = AssetDatabase.FindAssets("");
19         foreach (string guid in guids)
20         {
21             string assetPath = AssetDatabase.GUIDToAssetPath(guid);
22             string[] dependencies = AssetDatabase.GetDependencies(assetPath, false);
23 
24             foreach (var dependency in dependencies)
25             {
26                 if (referenceCache.ContainsKey(dependency))
27                 {
28                     if (!referenceCache[dependency].Contains(assetPath))
29                     {
30                         referenceCache[dependency].Add(assetPath);
31                     }
32                 }
33                 else
34                 {
35                     referenceCache[dependency] = new List<string>(){ assetPath };
36                 }
37             }
38         }
39 
40         Debug.Log("Build index takes " + sw.ElapsedMilliseconds + " milliseconds");
41 
42         string path = AssetDatabase.GetAssetPath(Selection.activeObject);
43         Debug.Log("Find: " + path, Selection.activeObject);
44         if (referenceCache.ContainsKey(path))
45         {
46             foreach (var reference in referenceCache[path])
47             {
48                 Debug.Log(reference, AssetDatabase.LoadMainAssetAtPath(reference));
49             }
50         }
51         else
52         {
53             Debug.LogWarning("No references");
54         }
55 
56         referenceCache.Clear();
57     }
58 
59     [MenuItem(MenuItemText, true)]
60     public static bool Validate()
61     {
62         if (Selection.activeObject)
63         {
64             string path = AssetDatabase.GetAssetPath(Selection.activeObject);
65             return !AssetDatabase.IsValidFolder(path);
66         }
67 
68         return false;
69     }
70 }

 

posted @ 2023-03-28 20:20  sun_dust_shadow  阅读(60)  评论(0编辑  收藏  举报