Skip to content

Commit

Permalink
外部调用、搜索meta引用
Browse files Browse the repository at this point in the history
  • Loading branch information
akof1314 committed Sep 3, 2024
1 parent 3b47531 commit b5c1db6
Show file tree
Hide file tree
Showing 13 changed files with 384 additions and 79 deletions.
48 changes: 48 additions & 0 deletions Documentation~/Guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,51 @@ private static AssetDanshariSetting OnCreateSetting()

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

## API 调用

每次需要拖曳文件/目录到窗口可能会觉得不方便,或者想要集成到自己的工具里,可以使用公开的 API,调用三个不同的窗口。

```csharp
/// <summary>
/// 显示引用查找窗口
/// </summary>
/// <param name="refPaths">引用的文件、目录集合</param>
/// <param name="resPaths">资源的文件、目录集合</param>
/// <param name="commonPaths">公共资源目录集合</param>
public static void DisplayReferenceWindow(string refPaths, string resPaths, string commonPaths = "")

/// <summary>
/// 显示被引用查找窗口
/// </summary>
public static void DisplayDependenciesWindow(string refPaths, string resPaths, string commonPaths = "")

/// <summary>
/// 显示重复资源检查窗口
/// </summary>
public static void DisplayDuplicateWindow(string refPaths, string resPaths, string commonPaths = "")
```

![](./Images/ApiDemo.gif)

示例工程举例实现右键资源文件来查看被引用情况。

```csharp
[MenuItem("Assets/查找该资源的被引用")]
private static void FindReferenceAll()
{
string[] paths = new string[Selection.objects.Length];
for (var i = 0; i < Selection.objects.Length; i++)
{
var o = Selection.objects[i];
paths[i] = AssetDatabase.GetAssetPath(o);
}

AssetDanshariWindow.DisplayDependenciesWindow(
AssetDanshariUtility.PathArrayToStr(new []{"Assets"}),
AssetDanshariUtility.PathArrayToStr(paths));
}
```

## 当前限制

二进制配置文件,如:Lighting data, Terrain data 之类,无法查找其引用资源关系。
Binary file added Documentation~/Images/ApiDemo.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion Editor/AssetDanshariHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class AssetDanshariHandler

public static Action<GenericMenu> onDependenciesContextDraw;

public static Action<string> onDependenciesFindItem;
internal static Action<string> onDependenciesFindItem;

public static Action<string, List<AssetTreeModel.AssetInfo>, AssetTreeModel> onDependenciesLoadDataMore;
}
Expand Down
75 changes: 74 additions & 1 deletion Editor/AssetDanshariSetting.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEditorInternal;
using UnityEngine;

namespace AssetDanshari
Expand All @@ -26,5 +28,76 @@ public List<AssetReferenceInfo> assetReferenceInfos
{
get { return m_AssetReferenceInfos; }
}

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

private static AssetDanshariSetting sSetting;

public static AssetDanshariSetting Get()
{
if (sSetting == null)
{
LoadSetting();
}

return sSetting;
}

private static void LoadSetting()
{
if (sSetting != null)
{
return;
}

UnityEngine.Object[] objects = InternalEditorUtility.LoadSerializedFileAndForget(kUserSettingsPath);
if (objects != null && objects.Length > 0)
{
sSetting = objects[0] as AssetDanshariSetting;
}
if (sSetting == null)
{
string[] guids = AssetDatabase.FindAssets("t:" + typeof(AssetDanshariSetting).Name);
if (guids.Length > 0)
{
string path = AssetDatabase.GUIDToAssetPath(guids[0]);
sSetting = AssetDatabase.LoadAssetAtPath<AssetDanshariSetting>(path);
}
}
if (sSetting == null)
{
if (AssetDanshariHandler.onCreateSetting != null)
{
sSetting = AssetDanshariHandler.onCreateSetting();
}
else
{
sSetting = CreateInstance<AssetDanshariSetting>();
}
SaveSetting();
}
}

public static void SaveSetting()
{
if (sSetting == null)
{
return;
}

var settingPath = AssetDatabase.GetAssetPath(sSetting);
if (!string.IsNullOrEmpty(settingPath))
{
return;
}

string folderPath = Path.GetDirectoryName(kUserSettingsPath);
if (!Directory.Exists(folderPath))
{
Directory.CreateDirectory(folderPath);
}

InternalEditorUtility.SaveToSerializedFileAndForget(new[] { sSetting }, kUserSettingsPath, true);
}
}
}
10 changes: 10 additions & 0 deletions Editor/AssetDanshariUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@ public static bool IsMetaExt(string ext)
return ext.EndsWith(".meta");
}

public static string GetPathFromTextMetaFilePath(string metaFile)
{
return metaFile.Substring(0, metaFile.Length - 5);
}

public static string GetTextMetaFilePathFromPath(string file)
{
return file + ".meta";
}

public static string GetSaveFilePath(string key)
{
string path = EditorPrefs.GetString("RecentSaveFilePath" + key, Application.dataPath + key + ".csv");
Expand Down
102 changes: 37 additions & 65 deletions Editor/AssetDanshariWindow.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.IO;
using UnityEditor;
using UnityEditor;
using UnityEditorInternal;
using UnityEngine;

Expand All @@ -13,13 +12,11 @@ static void ShowWindow()
GetWindow<AssetDanshariWindow>();
}

private AssetDanshariSetting m_AssetDanshariSetting;
private Vector2 m_ScrollViewVector2;
private ReorderableList m_ReorderableList;
private bool m_IsForceText;
private bool m_ShowGrepSetting;

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

private void Awake()
{
Expand All @@ -41,24 +38,24 @@ private void OnGUI()
EditorGUILayout.BeginHorizontal(EditorStyles.toolbar);
GUILayout.FlexibleSpace();
EditorGUI.BeginChangeCheck();
m_ShowGrepSetting = GUILayout.Toggle(m_ShowGrepSetting, string.IsNullOrEmpty(m_AssetDanshariSetting.ripgrepPath) ?
m_ShowGrepSetting = GUILayout.Toggle(m_ShowGrepSetting, string.IsNullOrEmpty(AssetDanshariSetting.Get().ripgrepPath) ?
style.grepNotSet : style.grepEnabled, EditorStyles.toolbarButton);
if (EditorGUI.EndChangeCheck())
{
SaveSetting();
AssetDanshariSetting.SaveSetting();
}
EditorGUILayout.EndHorizontal();
if (m_ShowGrepSetting)
{
EditorGUILayout.BeginHorizontal();
m_AssetDanshariSetting.ripgrepPath = EditorGUILayout.TextField(style.grepPath, m_AssetDanshariSetting.ripgrepPath);
AssetDanshariSetting.Get().ripgrepPath = EditorGUILayout.TextField(style.grepPath, AssetDanshariSetting.Get().ripgrepPath);
if (GUILayout.Button("O", GUILayout.ExpandWidth(false)))
{
var path = EditorUtility.OpenFilePanel(style.grepPath.text, "", "*");
if (!string.IsNullOrEmpty(path))
{
GUI.FocusControl(null);
m_AssetDanshariSetting.ripgrepPath = path;
AssetDanshariSetting.Get().ripgrepPath = path;
}
}
EditorGUILayout.EndHorizontal();
Expand All @@ -75,45 +72,18 @@ private void OnGUI()

private void Init()
{
if (m_AssetDanshariSetting == null)
if (m_ReorderableList == null)
{
m_IsForceText = EditorSettings.serializationMode == SerializationMode.ForceText;
if (!m_IsForceText)
{
return;
}

Object[] objects = InternalEditorUtility.LoadSerializedFileAndForget(kUserSettingsPath);
if (objects != null && objects.Length > 0)
{
m_AssetDanshariSetting = objects[0] as AssetDanshariSetting;
}
if (m_AssetDanshariSetting == null)
{
string[] guids = AssetDatabase.FindAssets("t:" + typeof(AssetDanshariSetting).Name);
if (guids.Length > 0)
{
string path = AssetDatabase.GUIDToAssetPath(guids[0]);
m_AssetDanshariSetting = AssetDatabase.LoadAssetAtPath<AssetDanshariSetting>(path);
}
}
if (m_AssetDanshariSetting == null)
{
if (AssetDanshariHandler.onCreateSetting != null)
{
m_AssetDanshariSetting = AssetDanshariHandler.onCreateSetting();
}
else
{
m_AssetDanshariSetting = CreateInstance<AssetDanshariSetting>();
}
SaveSetting();
}
}

if (m_ReorderableList == null)
{
m_ReorderableList = new ReorderableList(m_AssetDanshariSetting.assetReferenceInfos, null, true, true, true, true);
m_ReorderableList = new ReorderableList(AssetDanshariSetting.Get().assetReferenceInfos, null, true, true, true, true);
m_ReorderableList.drawHeaderCallback = OnDrawHeaderCallback;
m_ReorderableList.drawElementCallback = OnDrawElementCallback;
m_ReorderableList.elementHeight += 60;
Expand All @@ -127,13 +97,13 @@ private void OnDrawHeaderCallback(Rect rect)

private void OnDrawElementCallback(Rect rect, int index, bool isactive, bool isfocused)
{
if (m_AssetDanshariSetting == null || m_AssetDanshariSetting.assetReferenceInfos.Count < index)
if (AssetDanshariSetting.Get() == null || AssetDanshariSetting.Get().assetReferenceInfos.Count < index)
{
return;
}

var style = AssetDanshariStyle.Get();
var info = m_AssetDanshariSetting.assetReferenceInfos[index];
var info = AssetDanshariSetting.Get().assetReferenceInfos[index];
rect.height = EditorGUIUtility.singleLineHeight;
rect.y += 2;

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

rect2.y += EditorGUIUtility.singleLineHeight + 2;
Expand All @@ -167,15 +136,13 @@ private void OnDrawElementCallback(Rect rect, int index, bool isactive, bool isf
valueChanged |= EditorGUI.EndChangeCheck();
if (GUI.Button(rect4, style.assetReferenceCheckDup))
{
SaveSetting();
AssetBaseWindow.CheckPaths<AssetDuplicateWindow>(info.referenceFolder,
info.assetFolder, info.assetCommonFolder, m_AssetDanshariSetting.ripgrepPath);
AssetDanshariSetting.SaveSetting();
DisplayDuplicateWindow(info.referenceFolder, info.assetFolder, info.assetCommonFolder);
}
if (GUI.Button(rect5, style.assetReferenceDepend))
{
SaveSetting();
AssetBaseWindow.CheckPaths<AssetDependenciesWindow>(info.referenceFolder,
info.assetFolder, info.assetCommonFolder, m_AssetDanshariSetting.ripgrepPath);
AssetDanshariSetting.SaveSetting();
DisplayDependenciesWindow(info.referenceFolder, info.assetFolder, info.assetCommonFolder);
}

rect2.y += EditorGUIUtility.singleLineHeight + 2;
Expand Down Expand Up @@ -222,26 +189,31 @@ private string OnDrawElementAcceptDrop(Rect rect, string label)
return label;
}

private void SaveSetting()
/// <summary>
/// 显示引用查找窗口
/// </summary>
/// <param name="refPaths">引用的文件、目录集合</param>
/// <param name="resPaths">资源的文件、目录集合</param>
/// <param name="commonPaths">公共资源目录集合</param>
public static void DisplayReferenceWindow(string refPaths, string resPaths, string commonPaths = "")
{
if (m_AssetDanshariSetting == null)
{
return;
}

var settingPath = AssetDatabase.GetAssetPath(m_AssetDanshariSetting);
if (!string.IsNullOrEmpty(settingPath))
{
return;
}
AssetBaseWindow.CheckPaths<AssetReferenceWindow>(refPaths, resPaths, commonPaths, AssetDanshariSetting.Get().ripgrepPath);
}

string folderPath = Path.GetDirectoryName(kUserSettingsPath);
if (!Directory.Exists(folderPath))
{
Directory.CreateDirectory(folderPath);
}
/// <summary>
/// 显示被引用查找窗口
/// </summary>
public static void DisplayDependenciesWindow(string refPaths, string resPaths, string commonPaths = "")
{
AssetBaseWindow.CheckPaths<AssetDependenciesWindow>(refPaths, resPaths, commonPaths, AssetDanshariSetting.Get().ripgrepPath);
}

InternalEditorUtility.SaveToSerializedFileAndForget(new[] { m_AssetDanshariSetting }, kUserSettingsPath, true);
/// <summary>
/// 显示重复资源检查窗口
/// </summary>
public static void DisplayDuplicateWindow(string refPaths, string resPaths, string commonPaths = "")
{
AssetBaseWindow.CheckPaths<AssetDuplicateWindow>(refPaths, resPaths, commonPaths, AssetDanshariSetting.Get().ripgrepPath);
}
}
}
5 changes: 3 additions & 2 deletions Editor/DependenciesWindow/AssetDependenciesTreeModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ public override void SetDataPaths(string refPathStr, string pathStr, string comm
var resFileList = GetResFileList();
var guidList = GetGuidFromFileList(resFileList);
var fileList = GetRefFileList();
var refGuidMap = GetGuidMapFromFileList(fileList);
var searchRetList = GetSearchResultList(fileList.Count, guidList.Count);

ThreadDoFilesTextSearchReplace(grepPath, refPaths, fileList, guidList, String.Empty, searchRetList);
ThreadDoFilesTextSearchReplace(grepPath, refPaths, fileList, refGuidMap, guidList, String.Empty, searchRetList);
var rootInfo = DirToAssetInfoTree(resPaths);
var resInfos = FileListToAssetInfos(resFileList);

Expand Down Expand Up @@ -47,7 +48,7 @@ public override void SetDataPaths(string refPathStr, string pathStr, string comm
}
EditorUtility.ClearProgressBar();
}

public override void ExportCsv()
{
string path = AssetDanshariUtility.GetSaveFilePath(typeof(AssetDependenciesWindow).Name);
Expand Down
3 changes: 2 additions & 1 deletion Editor/DuplicateWindow/AssetDuplicateTreeModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,9 @@ public void SetUseThis(AssetInfo group, AssetInfo useInfo)

string replaceStr = AssetDatabase.AssetPathToGUID(useInfo.fileRelativePath);
List<string> fileList = GetRefFileList();
var refGuidMap = GetGuidMapFromFileList(fileList);

ThreadDoFilesTextSearchReplace(grepPath, refPaths, fileList, patterns, replaceStr, GetSearchResultList(fileList.Count, 0));
ThreadDoFilesTextSearchReplace(grepPath, refPaths, fileList, refGuidMap, patterns, replaceStr, GetSearchResultList(fileList.Count, 0));
EditorUtility.DisplayProgressBar(style.progressTitle, style.deleteFile, 0.98f);
SetRemoveAllOther(group, useInfo);
EditorUtility.ClearProgressBar();
Expand Down
Loading

0 comments on commit b5c1db6

Please sign in to comment.