介绍

在 Unity 中经常会需要

  • 通过资源路径查找对应的 GUID
  • 通过资源 GUID 查找对应的路径
  • 通过 Shader 名字查找 Shader

环境

  • Unity 5.6.6f2
  • Unity 2019.2.0f1
  • Windows 7
  • macOS 10.14.6

功能

  • 文本框支持输入多行内容进行批量查找。同时支持 Windows 与 macOS,支持不同换行符。
  • 适用于所有 Unity 版本,使用到的 API 没有变化过。

使用

将代码保存为 FindUtility.cs 放到 Editor 目录中。

Unity 菜单 Tools | FindUtility 打开查找窗口,在对应功能的文本框里输入要查找的内容,点击查找按钮即可。

Unity FindUtility Window Snapshot

查找结果文本输出

1
2
Assets/Runtime/Control.cs | GUID: 004a3ecfcbbd3e541af85a83129ab80f
Resources/unity_builtin_extra | GUID: 0000000000000000f000000000000000 | Shader: Standard

代码

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
using System;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using Object = UnityEngine.Object;

public class FindUtility : EditorWindow
{
    [MenuItem("Tools/FindUtility")]
    public static void ShowWindow()
    {
        var window = GetWindow(typeof(FindUtility));
        window.minSize = new Vector2(400f, 400f);
        window.Show();
    }

    private void OnGUI()
    {
        GUILayout.Box("Asset");
        _assetPathText = EditorGUILayout.TextArea(_assetPathText, GUILayout.MinHeight(40f));
        if (GUILayout.Button("Convert to GUID"))
        {
            string[] assetPaths = _assetPathText.Split(StringSeparators, StringSplitOptions.RemoveEmptyEntries);
            OutputAssetPaths(assetPaths, null, null);
        }

        GUILayout.Space(20f);
        GUILayout.Box("GUID");
        _guidText = EditorGUILayout.TextArea(_guidText, GUILayout.MinHeight(40f));
        if (GUILayout.Button("Convert to AssetPath"))
        {
            string[] guids = _guidText.Split(StringSeparators, StringSplitOptions.RemoveEmptyEntries);
            string[] assetPaths = new string[guids.Length];
            for (int i = 0; i < guids.Length; i++)
            {
                assetPaths[i] = AssetDatabase.GUIDToAssetPath(guids[i]);
            }

            OutputAssetPaths(assetPaths, null, null);
        }

        GUILayout.Space(20f);
        GUILayout.Box("Shader");
        _shaderText = EditorGUILayout.TextArea(_shaderText, GUILayout.MinHeight(40f));
        if (GUILayout.Button("Find"))
        {
            string[] shaders = _shaderText.Split(StringSeparators, StringSplitOptions.RemoveEmptyEntries);
            string[] assetPaths = new string[shaders.Length];
            for (int i = 0; i < shaders.Length; i++)
            {
                var shader = Shader.Find(shaders[i]);
                if (shader != null)
                {
                    assetPaths[i] = AssetDatabase.GetAssetPath(shader);
                }
                else
                {
                    assetPaths[i] = null;
                }
            }

            OutputAssetPaths(assetPaths, shaders, "Shader");
        }
    }

    private void OutputAssetPaths(string[] assetPaths, string[] extraInfos, string extraHeader)
    {
        var selected = new List<Object>(assetPaths.Length);
        for (int i = 0; i < assetPaths.Length; i++)
        {
            string assetPath = assetPaths[i];
            if (string.IsNullOrEmpty(assetPath))
            {
                continue;
            }

            if (assetPath.EndsWith(MetaExtension))
            {
                assetPath = assetPath.Substring(0, assetPath.Length - MetaExtension.Length);
            }

            string guid = AssetDatabase.AssetPathToGUID(assetPath);
            if (string.IsNullOrEmpty(guid))
            {
                Debug.LogError(string.Format("{0} didn't exist", assetPath));
                continue;
            }

            var asset = AssetDatabase.LoadMainAssetAtPath(assetPath);
            selected.Add(asset);
            if (string.IsNullOrEmpty(extraHeader))
            {
                Debug.Log(string.Format("{0} | GUID: {1}", assetPath, guid), asset);
            }
            else
            {
                Debug.Log(string.Format("{0} | GUID: {1} | {2}: {3}", assetPath, guid, extraHeader, extraInfos[i]),
                    asset);
            }
        }

        Selection.objects = selected.ToArray();
    }

    private const string MetaExtension = ".meta";
    private readonly char[] StringSeparators = {'\r', '\n'};
    private string _assetPathText;
    private string _guidText;
    private string _shaderText;
}