Skip to content

Commit b5c1db6

Browse files
committed
外部调用、搜索meta引用
1 parent 3b47531 commit b5c1db6

File tree

13 files changed

+384
-79
lines changed

13 files changed

+384
-79
lines changed

Documentation~/Guide.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,3 +93,51 @@ private static AssetDanshariSetting OnCreateSetting()
9393

9494
资源不止被另外的资源直接引用,还可能配置在表里、代码里进行动态使用,在这种情况下,可以绑定`onDependenciesLoadDataMore`事件,对传入的资源路径进行判别处理,参照示例工程代码。
9595

96+
## API 调用
97+
98+
每次需要拖曳文件/目录到窗口可能会觉得不方便,或者想要集成到自己的工具里,可以使用公开的 API,调用三个不同的窗口。
99+
100+
```csharp
101+
/// <summary>
102+
/// 显示引用查找窗口
103+
/// </summary>
104+
/// <param name="refPaths">引用的文件、目录集合</param>
105+
/// <param name="resPaths">资源的文件、目录集合</param>
106+
/// <param name="commonPaths">公共资源目录集合</param>
107+
public static void DisplayReferenceWindow(string refPaths, string resPaths, string commonPaths = "")
108+
109+
/// <summary>
110+
/// 显示被引用查找窗口
111+
/// </summary>
112+
public static void DisplayDependenciesWindow(string refPaths, string resPaths, string commonPaths = "")
113+
114+
/// <summary>
115+
/// 显示重复资源检查窗口
116+
/// </summary>
117+
public static void DisplayDuplicateWindow(string refPaths, string resPaths, string commonPaths = "")
118+
```
119+
120+
![](./Images/ApiDemo.gif)
121+
122+
示例工程举例实现右键资源文件来查看被引用情况。
123+
124+
```csharp
125+
[MenuItem("Assets/查找该资源的被引用")]
126+
private static void FindReferenceAll()
127+
{
128+
string[] paths = new string[Selection.objects.Length];
129+
for (var i = 0; i < Selection.objects.Length; i++)
130+
{
131+
var o = Selection.objects[i];
132+
paths[i] = AssetDatabase.GetAssetPath(o);
133+
}
134+
135+
AssetDanshariWindow.DisplayDependenciesWindow(
136+
AssetDanshariUtility.PathArrayToStr(new []{"Assets"}),
137+
AssetDanshariUtility.PathArrayToStr(paths));
138+
}
139+
```
140+
141+
## 当前限制
142+
143+
二进制配置文件,如:Lighting data, Terrain data 之类,无法查找其引用资源关系。

Documentation~/Images/ApiDemo.gif

57.1 KB
Loading

Editor/AssetDanshariHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public class AssetDanshariHandler
1010

1111
public static Action<GenericMenu> onDependenciesContextDraw;
1212

13-
public static Action<string> onDependenciesFindItem;
13+
internal static Action<string> onDependenciesFindItem;
1414

1515
public static Action<string, List<AssetTreeModel.AssetInfo>, AssetTreeModel> onDependenciesLoadDataMore;
1616
}

Editor/AssetDanshariSetting.cs

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using System;
2-
using System.Collections;
32
using System.Collections.Generic;
3+
using System.IO;
4+
using UnityEditor;
5+
using UnityEditorInternal;
46
using UnityEngine;
57

68
namespace AssetDanshari
@@ -26,5 +28,76 @@ public List<AssetReferenceInfo> assetReferenceInfos
2628
{
2729
get { return m_AssetReferenceInfos; }
2830
}
31+
32+
private static readonly string kUserSettingsPath = "UserSettings/AssetDanshariSetting.asset";
33+
34+
private static AssetDanshariSetting sSetting;
35+
36+
public static AssetDanshariSetting Get()
37+
{
38+
if (sSetting == null)
39+
{
40+
LoadSetting();
41+
}
42+
43+
return sSetting;
44+
}
45+
46+
private static void LoadSetting()
47+
{
48+
if (sSetting != null)
49+
{
50+
return;
51+
}
52+
53+
UnityEngine.Object[] objects = InternalEditorUtility.LoadSerializedFileAndForget(kUserSettingsPath);
54+
if (objects != null && objects.Length > 0)
55+
{
56+
sSetting = objects[0] as AssetDanshariSetting;
57+
}
58+
if (sSetting == null)
59+
{
60+
string[] guids = AssetDatabase.FindAssets("t:" + typeof(AssetDanshariSetting).Name);
61+
if (guids.Length > 0)
62+
{
63+
string path = AssetDatabase.GUIDToAssetPath(guids[0]);
64+
sSetting = AssetDatabase.LoadAssetAtPath<AssetDanshariSetting>(path);
65+
}
66+
}
67+
if (sSetting == null)
68+
{
69+
if (AssetDanshariHandler.onCreateSetting != null)
70+
{
71+
sSetting = AssetDanshariHandler.onCreateSetting();
72+
}
73+
else
74+
{
75+
sSetting = CreateInstance<AssetDanshariSetting>();
76+
}
77+
SaveSetting();
78+
}
79+
}
80+
81+
public static void SaveSetting()
82+
{
83+
if (sSetting == null)
84+
{
85+
return;
86+
}
87+
88+
var settingPath = AssetDatabase.GetAssetPath(sSetting);
89+
if (!string.IsNullOrEmpty(settingPath))
90+
{
91+
return;
92+
}
93+
94+
string folderPath = Path.GetDirectoryName(kUserSettingsPath);
95+
if (!Directory.Exists(folderPath))
96+
{
97+
Directory.CreateDirectory(folderPath);
98+
}
99+
100+
InternalEditorUtility.SaveToSerializedFileAndForget(new[] { sSetting }, kUserSettingsPath, true);
101+
}
29102
}
30103
}

Editor/AssetDanshariUtility.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,16 @@ public static bool IsMetaExt(string ext)
3434
return ext.EndsWith(".meta");
3535
}
3636

37+
public static string GetPathFromTextMetaFilePath(string metaFile)
38+
{
39+
return metaFile.Substring(0, metaFile.Length - 5);
40+
}
41+
42+
public static string GetTextMetaFilePathFromPath(string file)
43+
{
44+
return file + ".meta";
45+
}
46+
3747
public static string GetSaveFilePath(string key)
3848
{
3949
string path = EditorPrefs.GetString("RecentSaveFilePath" + key, Application.dataPath + key + ".csv");

Editor/AssetDanshariWindow.cs

Lines changed: 37 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using System.IO;
2-
using UnityEditor;
1+
using UnityEditor;
32
using UnityEditorInternal;
43
using UnityEngine;
54

@@ -13,13 +12,11 @@ static void ShowWindow()
1312
GetWindow<AssetDanshariWindow>();
1413
}
1514

16-
private AssetDanshariSetting m_AssetDanshariSetting;
1715
private Vector2 m_ScrollViewVector2;
1816
private ReorderableList m_ReorderableList;
1917
private bool m_IsForceText;
2018
private bool m_ShowGrepSetting;
2119

22-
private static readonly string kUserSettingsPath = "UserSettings/AssetDanshariSetting.asset";
2320

2421
private void Awake()
2522
{
@@ -41,24 +38,24 @@ private void OnGUI()
4138
EditorGUILayout.BeginHorizontal(EditorStyles.toolbar);
4239
GUILayout.FlexibleSpace();
4340
EditorGUI.BeginChangeCheck();
44-
m_ShowGrepSetting = GUILayout.Toggle(m_ShowGrepSetting, string.IsNullOrEmpty(m_AssetDanshariSetting.ripgrepPath) ?
41+
m_ShowGrepSetting = GUILayout.Toggle(m_ShowGrepSetting, string.IsNullOrEmpty(AssetDanshariSetting.Get().ripgrepPath) ?
4542
style.grepNotSet : style.grepEnabled, EditorStyles.toolbarButton);
4643
if (EditorGUI.EndChangeCheck())
4744
{
48-
SaveSetting();
45+
AssetDanshariSetting.SaveSetting();
4946
}
5047
EditorGUILayout.EndHorizontal();
5148
if (m_ShowGrepSetting)
5249
{
5350
EditorGUILayout.BeginHorizontal();
54-
m_AssetDanshariSetting.ripgrepPath = EditorGUILayout.TextField(style.grepPath, m_AssetDanshariSetting.ripgrepPath);
51+
AssetDanshariSetting.Get().ripgrepPath = EditorGUILayout.TextField(style.grepPath, AssetDanshariSetting.Get().ripgrepPath);
5552
if (GUILayout.Button("O", GUILayout.ExpandWidth(false)))
5653
{
5754
var path = EditorUtility.OpenFilePanel(style.grepPath.text, "", "*");
5855
if (!string.IsNullOrEmpty(path))
5956
{
6057
GUI.FocusControl(null);
61-
m_AssetDanshariSetting.ripgrepPath = path;
58+
AssetDanshariSetting.Get().ripgrepPath = path;
6259
}
6360
}
6461
EditorGUILayout.EndHorizontal();
@@ -75,45 +72,18 @@ private void OnGUI()
7572

7673
private void Init()
7774
{
78-
if (m_AssetDanshariSetting == null)
75+
if (m_ReorderableList == null)
7976
{
8077
m_IsForceText = EditorSettings.serializationMode == SerializationMode.ForceText;
8178
if (!m_IsForceText)
8279
{
8380
return;
8481
}
85-
86-
Object[] objects = InternalEditorUtility.LoadSerializedFileAndForget(kUserSettingsPath);
87-
if (objects != null && objects.Length > 0)
88-
{
89-
m_AssetDanshariSetting = objects[0] as AssetDanshariSetting;
90-
}
91-
if (m_AssetDanshariSetting == null)
92-
{
93-
string[] guids = AssetDatabase.FindAssets("t:" + typeof(AssetDanshariSetting).Name);
94-
if (guids.Length > 0)
95-
{
96-
string path = AssetDatabase.GUIDToAssetPath(guids[0]);
97-
m_AssetDanshariSetting = AssetDatabase.LoadAssetAtPath<AssetDanshariSetting>(path);
98-
}
99-
}
100-
if (m_AssetDanshariSetting == null)
101-
{
102-
if (AssetDanshariHandler.onCreateSetting != null)
103-
{
104-
m_AssetDanshariSetting = AssetDanshariHandler.onCreateSetting();
105-
}
106-
else
107-
{
108-
m_AssetDanshariSetting = CreateInstance<AssetDanshariSetting>();
109-
}
110-
SaveSetting();
111-
}
11282
}
11383

11484
if (m_ReorderableList == null)
11585
{
116-
m_ReorderableList = new ReorderableList(m_AssetDanshariSetting.assetReferenceInfos, null, true, true, true, true);
86+
m_ReorderableList = new ReorderableList(AssetDanshariSetting.Get().assetReferenceInfos, null, true, true, true, true);
11787
m_ReorderableList.drawHeaderCallback = OnDrawHeaderCallback;
11888
m_ReorderableList.drawElementCallback = OnDrawElementCallback;
11989
m_ReorderableList.elementHeight += 60;
@@ -127,13 +97,13 @@ private void OnDrawHeaderCallback(Rect rect)
12797

12898
private void OnDrawElementCallback(Rect rect, int index, bool isactive, bool isfocused)
12999
{
130-
if (m_AssetDanshariSetting == null || m_AssetDanshariSetting.assetReferenceInfos.Count < index)
100+
if (AssetDanshariSetting.Get() == null || AssetDanshariSetting.Get().assetReferenceInfos.Count < index)
131101
{
132102
return;
133103
}
134104

135105
var style = AssetDanshariStyle.Get();
136-
var info = m_AssetDanshariSetting.assetReferenceInfos[index];
106+
var info = AssetDanshariSetting.Get().assetReferenceInfos[index];
137107
rect.height = EditorGUIUtility.singleLineHeight;
138108
rect.y += 2;
139109

@@ -151,9 +121,8 @@ private void OnDrawElementCallback(Rect rect, int index, bool isactive, bool isf
151121
bool valueChanged = EditorGUI.EndChangeCheck();
152122
if (GUI.Button(rect4, style.assetReferenceCheckRef))
153123
{
154-
SaveSetting();
155-
AssetBaseWindow.CheckPaths<AssetReferenceWindow>(info.referenceFolder,
156-
info.assetFolder, info.assetCommonFolder, m_AssetDanshariSetting.ripgrepPath);
124+
AssetDanshariSetting.SaveSetting();
125+
DisplayReferenceWindow(info.referenceFolder, info.assetFolder, info.assetCommonFolder);
157126
}
158127

159128
rect2.y += EditorGUIUtility.singleLineHeight + 2;
@@ -167,15 +136,13 @@ private void OnDrawElementCallback(Rect rect, int index, bool isactive, bool isf
167136
valueChanged |= EditorGUI.EndChangeCheck();
168137
if (GUI.Button(rect4, style.assetReferenceCheckDup))
169138
{
170-
SaveSetting();
171-
AssetBaseWindow.CheckPaths<AssetDuplicateWindow>(info.referenceFolder,
172-
info.assetFolder, info.assetCommonFolder, m_AssetDanshariSetting.ripgrepPath);
139+
AssetDanshariSetting.SaveSetting();
140+
DisplayDuplicateWindow(info.referenceFolder, info.assetFolder, info.assetCommonFolder);
173141
}
174142
if (GUI.Button(rect5, style.assetReferenceDepend))
175143
{
176-
SaveSetting();
177-
AssetBaseWindow.CheckPaths<AssetDependenciesWindow>(info.referenceFolder,
178-
info.assetFolder, info.assetCommonFolder, m_AssetDanshariSetting.ripgrepPath);
144+
AssetDanshariSetting.SaveSetting();
145+
DisplayDependenciesWindow(info.referenceFolder, info.assetFolder, info.assetCommonFolder);
179146
}
180147

181148
rect2.y += EditorGUIUtility.singleLineHeight + 2;
@@ -222,26 +189,31 @@ private string OnDrawElementAcceptDrop(Rect rect, string label)
222189
return label;
223190
}
224191

225-
private void SaveSetting()
192+
/// <summary>
193+
/// 显示引用查找窗口
194+
/// </summary>
195+
/// <param name="refPaths">引用的文件、目录集合</param>
196+
/// <param name="resPaths">资源的文件、目录集合</param>
197+
/// <param name="commonPaths">公共资源目录集合</param>
198+
public static void DisplayReferenceWindow(string refPaths, string resPaths, string commonPaths = "")
226199
{
227-
if (m_AssetDanshariSetting == null)
228-
{
229-
return;
230-
}
231-
232-
var settingPath = AssetDatabase.GetAssetPath(m_AssetDanshariSetting);
233-
if (!string.IsNullOrEmpty(settingPath))
234-
{
235-
return;
236-
}
200+
AssetBaseWindow.CheckPaths<AssetReferenceWindow>(refPaths, resPaths, commonPaths, AssetDanshariSetting.Get().ripgrepPath);
201+
}
237202

238-
string folderPath = Path.GetDirectoryName(kUserSettingsPath);
239-
if (!Directory.Exists(folderPath))
240-
{
241-
Directory.CreateDirectory(folderPath);
242-
}
203+
/// <summary>
204+
/// 显示被引用查找窗口
205+
/// </summary>
206+
public static void DisplayDependenciesWindow(string refPaths, string resPaths, string commonPaths = "")
207+
{
208+
AssetBaseWindow.CheckPaths<AssetDependenciesWindow>(refPaths, resPaths, commonPaths, AssetDanshariSetting.Get().ripgrepPath);
209+
}
243210

244-
InternalEditorUtility.SaveToSerializedFileAndForget(new[] { m_AssetDanshariSetting }, kUserSettingsPath, true);
211+
/// <summary>
212+
/// 显示重复资源检查窗口
213+
/// </summary>
214+
public static void DisplayDuplicateWindow(string refPaths, string resPaths, string commonPaths = "")
215+
{
216+
AssetBaseWindow.CheckPaths<AssetDuplicateWindow>(refPaths, resPaths, commonPaths, AssetDanshariSetting.Get().ripgrepPath);
245217
}
246218
}
247219
}

Editor/DependenciesWindow/AssetDependenciesTreeModel.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@ public override void SetDataPaths(string refPathStr, string pathStr, string comm
1414
var resFileList = GetResFileList();
1515
var guidList = GetGuidFromFileList(resFileList);
1616
var fileList = GetRefFileList();
17+
var refGuidMap = GetGuidMapFromFileList(fileList);
1718
var searchRetList = GetSearchResultList(fileList.Count, guidList.Count);
1819

19-
ThreadDoFilesTextSearchReplace(grepPath, refPaths, fileList, guidList, String.Empty, searchRetList);
20+
ThreadDoFilesTextSearchReplace(grepPath, refPaths, fileList, refGuidMap, guidList, String.Empty, searchRetList);
2021
var rootInfo = DirToAssetInfoTree(resPaths);
2122
var resInfos = FileListToAssetInfos(resFileList);
2223

@@ -47,7 +48,7 @@ public override void SetDataPaths(string refPathStr, string pathStr, string comm
4748
}
4849
EditorUtility.ClearProgressBar();
4950
}
50-
51+
5152
public override void ExportCsv()
5253
{
5354
string path = AssetDanshariUtility.GetSaveFilePath(typeof(AssetDependenciesWindow).Name);

Editor/DuplicateWindow/AssetDuplicateTreeModel.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,8 +231,9 @@ public void SetUseThis(AssetInfo group, AssetInfo useInfo)
231231

232232
string replaceStr = AssetDatabase.AssetPathToGUID(useInfo.fileRelativePath);
233233
List<string> fileList = GetRefFileList();
234+
var refGuidMap = GetGuidMapFromFileList(fileList);
234235

235-
ThreadDoFilesTextSearchReplace(grepPath, refPaths, fileList, patterns, replaceStr, GetSearchResultList(fileList.Count, 0));
236+
ThreadDoFilesTextSearchReplace(grepPath, refPaths, fileList, refGuidMap, patterns, replaceStr, GetSearchResultList(fileList.Count, 0));
236237
EditorUtility.DisplayProgressBar(style.progressTitle, style.deleteFile, 0.98f);
237238
SetRemoveAllOther(group, useInfo);
238239
EditorUtility.ClearProgressBar();

0 commit comments

Comments
 (0)