查找组件上的引用、依赖

查找组件上的引用、依赖
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using UnityEditor.Sprites;
using UnityEngine.Tilemaps;

public class FindCite : Editor
{

    [MenuItem("Assets/FindCite")]
    static void FindAllCite()
    {
        if (GetSelectedAssetPath() == null) return;
        GameObject go = GetSelectedAssetPath() as GameObject;
        //查找SpriteRenderer组件
        SpriteRenderer[] sprenderers = go.GetComponentsInChildren<SpriteRenderer>(true);
        foreach (var item in sprenderers)
        {
            if (item.sprite != null)//是否 有引用的图片
            {
            //打印挂载图片物体名字  图片图集名字   图片路径
                GameDebugger.instance.GameDebugLog("SpriteRenderName:  ", item.name, "  SpriteAlas:  ", SpriteUtility.GetSpriteTexture(item.sprite, true).name,"SpriteName:  ",AssetDatabase.GetAssetPath(item.sprite));
            }
        }

        TilemapRenderer[] tilemap = go.GetComponentsInChildren<TilemapRenderer>(true);
        foreach (var item in tilemap)
        {
            if (item.sharedMaterial!= null)
            {
                GameDebugger.instance.GameDebugLog("TileMapRendererName:", item.gameObject.name, "  MaterialName:", AssetDatabase.GetAssetPath(item.sharedMaterial));
            }
        }
        Animator[] animators = go.GetComponentsInChildren<Animator>(true);
        foreach (var item in animators)
        {
            if (item.runtimeAnimatorController != null)
            {
               GameDebugger.instance.GameDebugLog("AnimatorGameName:", item.gameObject.name, "  AnimatorName:",AssetDatabase.GetAssetPath(item.runtimeAnimatorController));
            }
        }
    }
    
     [MenuItem("Assets/GetDependencies")]
    static void GetDependencies()
    {
        Object selected = Selection.activeObject;//所选着的预制体
        string selectedPath = AssetDatabase.GetAssetPath(selected);//获取预设体的路径
        Debug.LogError(selectedPath);
        string[] dep = AssetDatabase.GetDependencies(selectedPath, true);//获取预设体所有的依赖
        foreach (string assetName in dep)
        {
            Debug.LogError(assetName);//打印依赖资源路径
        }

    }
    static UnityEngine.Object GetSelectedAssetPath()
    {
     var selection=   Selection.activeObject;
     if (selection != null)
     {
         GameDebugger.instance.GameDebugLog(selection.GetType());
         if (selection is GameObject)
         {
             return selection;
         }
         else
         {
             return null;
         }
     }
     else
     {
         return null;
     }

   
    }
}

使用方法,选着物体右键FindCite,信息就会打印在控制台上,GameDebugger类是自己封装的,也可以用Debug.Log(),
脚本内的组件名字都可以根据自己情况替换

GetDependencies()可以查找预设体所有的引用,可以打印出完整的依赖资源的路径