See which PreFab references are made by Sprite

There are often reused or duplicate resource images in the project. When arranging, you want to check which Prefab images are referenced for safe removal. You can use the AssetDataBase API to find the GUID of a resource, which is unique to each resource after it is imported. View all Prefab references with GUIDS in the entire AssetDatabase based on guIDS.

In the past, the Prefab name was typed directly into log, and the manual copy search was tedious. A tool found on the Internet, very easy to use, you can directly click view jump, using the ObjectField of EditorGUI

code

public class SearchReferenceEditorWindow : EditorWindow{[MenuItem("Assets/SearchReference")]
    static void SearchRef()
    {
        SearchReferenceEditorWindow window =
            (SearchReferenceEditorWindow) EditorWindow.GetWindow(typeof(SearchReferenceEditorWindow), false."Searching".true);
    }

    private static Object _searchObject;
    private List<Object> _resultList = new List<Object>();

    private void OnGUI()
    {
        EditorGUILayout.BeginHorizontal();
        _searchObject = EditorGUILayout.ObjectField(_searchObject, typeof(Object), true, GUILayout.Width(200));
        if (GUILayout.Button("Search", GUILayout.Width(100)))
        {
            _resultList.Clear();
            if (_searchObject == null)
            {
                return;
            }

            string assetPath = AssetDatabase.GetAssetPath(_searchObject); // Path to obtain the resource
            string assetGuid = AssetDatabase.AssetPathToGUID(assetPath); // Get the GUID of the resource

            // / Go through all the objects in the project, the first parameter search variable, the second parameter search folder directory
            string[] guids = AssetDatabase.FindAssets("t:Prefab".new[] {"Assets"});
            int length = guids.Length;
            for (int i = 0; i < length; i++)
            {
                string filePath = AssetDatabase.GUIDToAssetPath(guids[i]); // Get each Prefab path
                EditorUtility.DisplayCancelableProgressBar("Checking", filePath, i / length * 1.0 f);
                string content = File.ReadAllText(filePath); // Find references in prefab files
                if (content.Contains(assetGuid))
                {
                    Object fileObject = AssetDatabase.LoadAssetAtPath(filePath, typeof(Object)); // Load Prefab onto ObjectField
                    _resultList.Add(fileObject);
                }

                EditorUtility.ClearProgressBar();
            }
        }
        EditorGUILayout.EndHorizontal();

        EditorGUILayout.BeginVertical();
        foreach (var obj in _resultList)
        {
            EditorGUILayout.ObjectField(obj, typeof(Object), true, GUILayout.Width(300)); } EditorGUILayout.EndVertical(); }}Copy the code