利用Meta文件的GUID查找资源引用

通过AssetDatabase.GetAssetPath可以获取到当前对象在项目的路径

AssetDatabase.AssetPathToGUID(通过路径获取到GUID),其实我觉得本质上就是访问它的Meta文件,然后得到Meta信息中的GUID字符串

对于Prefab Unity Material  这些Unity的非二进制文件,一般都显示包含了其配置信息,可以直接拿文本编辑器查看。

总结:

得到GUID->遍历Assets下的所有.prefab、.unity、.mat、.asset等unity文件=》匹配GUID=》输出所有包含GUID的对象。

具体文章:点击跳转

public class FindReferences
{
  
    [MenuItem("Assets/Find References", false, 10)]
    static private void Find()
    {
        EditorSettings.serializationMode = SerializationMode.ForceText;
        string path = AssetDatabase.GetAssetPath(Selection.activeObject);
        if (!string.IsNullOrEmpty(path))
        {
            string guid = AssetDatabase.AssetPathToGUID(path);
            List withoutExtensions = new List(){".prefab",".unity",".mat",".asset"};
            string[] files = Directory.GetFiles(Application.dataPath, "*.*", SearchOption.AllDirectories)
                .Where(s => withoutExtensions.Contains(Path.GetExtension(s).ToLower())).ToArray();
            int startIndex = 0;
 
            EditorApplication.update = delegate()
            {
                string file = files[startIndex];
            
                 bool isCancel = EditorUtility.DisplayCancelableProgressBar("匹配资源中", file, (float)startIndex / (float)files.Length);
 
                if (Regex.IsMatch(File.ReadAllText(file), guid))
                {
                    Debug.Log(file, AssetDatabase.LoadAssetAtPath<Object>(GetRelativeAssetsPath(file)));
                }
 
                startIndex++;
                if (isCancel || startIndex >= files.Length)
                {
                    EditorUtility.ClearProgressBar();
                    EditorApplication.update = null;
                    startIndex = 0;
                    Debug.Log("匹配结束");
                }
 
            };
        }
    }
 
    [MenuItem("Assets/Find References", true)]
    static private bool VFind()
    {
        string path = AssetDatabase.GetAssetPath(Selection.activeObject);
        return (!string.IsNullOrEmpty(path));
    }
 
    static private string GetRelativeAssetsPath(string path)
    {
        return "Assets" + Path.GetFullPath(path).Replace(Path.GetFullPath(Application.dataPath), "").Replace('\\', '/');
    }
}

 

作者:Miracle
来源:麦瑞克博客
链接:https://www.playcreator.cn/archives/unity/1424/
本博客所有文章除特别声明外,均采用CC BY-NC-SA 4.0许可协议,转载请注明!
THE END
分享
海报
利用Meta文件的GUID查找资源引用
通过AssetDatabase.GetAssetPath可以获取到当前对象在项目的路径 AssetDatabase.AssetPathToGUID(通过路径获取到GUID),其实我觉得本质上就是访问它的Meta文……
<<上一篇
下一篇>>
文章目录
关闭
目 录