using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
namespace DevKit.Utility.Editor
{
public class FindAssetByGuid : EditorWindow
{
private string _guidInput = string.Empty;
private Object _asset;
code
private List<Object> _assetRefs = new();
private Vector2 _assetRefsScroll = Vector2.zero;
[MenuItem("DevKit/Utility/FindAssetByGuid")]
public static void ShowWindow()
{
GetWindow<FindAssetByGuid>("Find Asset by GUID");
}
private void OnGUI()
{
GUILayout.Label("Asset Finder", EditorStyles.boldLabel);
GUILayout.Label("Enter GUID:", EditorStyles.label);
_guidInput = EditorGUILayout.TextField(_guidInput);
if (GUILayout.Button("Find Asset"))
{
if (!string.IsNullOrEmpty(_guidInput))
{
string assetPath = AssetDatabase.GUIDToAssetPath(_guidInput);
if (!string.IsNullOrEmpty(assetPath))
{
_asset = AssetDatabase.LoadAssetAtPath<Object>(assetPath);
if (_asset != null)
{
Debug.Log($"Asset found at path: {assetPath}");
}
else
{
Debug.LogWarning("Asset could not be loaded.");
}
}
else
{
Debug.LogWarning("No asset found with the specified GUID.");
_asset = null;
}
}
}
GUILayout.Space(10);
EditorGUILayout.ObjectField("Asset", _asset, typeof(Object), false);
while (GUILayout.Button("FindReferences"))
{
if (_asset == null)
break;
// 查找所有引用了当前资源的资源
_assetRefs.Clear();
break;
}
}
}
Assistant
} please finish "FindReferences"
To complete the FindReferences functionality, you will need to search through the assets in your project to find which ones reference the given asset. This involves going through each asset in the project, loading its dependencies, and checking if your target asset is among them.
Here's an extended version of your script with a simple implementation of the FindReferences feature:
csharp
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
namespace DevKit.Utility.Editor
{
public class FindAssetByGuid : EditorWindow
{
private string _guidInput = string.Empty;
private Object _asset;
private List<Object> _assetRefs = new();
private Vector2 _assetRefsScroll = Vector2.zero;
[MenuItem("DevKit/Utility/FindAssetByGuid")]
public static void ShowWindow()
{
GetWindow<FindAssetByGuid>("Find Asset by GUID");
}
private void OnGUI()
{
GUILayout.Label("Asset Finder", EditorStyles.boldLabel);
GUILayout.Label("Enter GUID:", EditorStyles.label);
_guidInput = EditorGUILayout.TextField(_guidInput);
if (GUILayout.Button("Find Asset"))
{
if (!string.IsNullOrEmpty(_guidInput))
{
string assetPath = AssetDatabase.GUIDToAssetPath(_guidInput);
if (!string.IsNullOrEmpty(assetPath))
{
_asset = AssetDatabase.LoadAssetAtPath<Object>(assetPath);
if (_asset != null)
{
Debug.Log($"Asset found at path: {assetPath}");
}
else
{
Debug.LogWarning("Asset could not be loaded.");
}
}
else
{
Debug.LogWarning("No asset found with the specified GUID.");
_asset = null;
}
}
}
GUILayout.Space(10);
EditorGUILayout.ObjectField("Asset", _asset, typeof(Object), false);
if (GUILayout.Button("Find References"))
{
if (_asset != null)
{
FindReferences();
}
}
if (_assetRefs.Count > 0)
{
GUILayout.Label("Asset References:", EditorStyles.boldLabel);
_assetRefsScroll = GUILayout.BeginScrollView(_assetRefsScroll, GUILayout.Height(150));
foreach (var assetRef in _assetRefs)
{
if (assetRef != null)
{
EditorGUILayout.ObjectField(assetRef, typeof(Object), false);
}
}
GUILayout.EndScrollView();
}
}
private void FindReferences()
{
_assetRefs.Clear();
string[] allAssetPaths = AssetDatabase.GetAllAssetPaths();
foreach (var path in allAssetPaths)
{
var dependencies = AssetDatabase.GetDependencies(path, true);
foreach (var dependency in dependencies)
{
if (dependency == AssetDatabase.GetAssetPath(_asset))
{
var referencingAsset = AssetDatabase.LoadAssetAtPath<Object>(path);
if (referencingAsset != null && !_assetRefs.Contains(referencingAsset))
{
_assetRefs.Add(referencingAsset);
}
break;
}
}
}
if (_assetRefs.Count > 0)
{
Debug.Log($"{_assetRefs.Count} assets reference the specified asset.");
}
else
{
Debug.Log("No assets reference the specified asset.");
}
}
}
}
Explanation:
FindReferences Method: This method iterates over all asset paths in the project using AssetDatabase.GetAllAssetPaths. For each asset, it retrieves all its dependencies using AssetDatabase.GetDependencies. If one of the dependencies matches the path of the target asset, the asset is added to the _assetRefs list.
UI Update: After finding references, the results are displayed in a scrollable list below the "Find References" button.
This script uses basic asset path checking to determine references, which should work for many types of assets. However, keep in mind that this method may not cover some indirect references or complex asset types.