unity查找图片引用(一)

最近做项目的时候发现个问题,unity里面放的图多了,找到引用的预制体很麻烦,想删图片又怕删到引用的。。。所以想着搞个查找引用的操作,记录一下

大概的思路是:选择要查找的图片,右键---查找,然后把查找出有引用该图片的预制体的路径Debug出来;

网上查了下,我试过了,下面这个复制过去,改下参数就可以用了;

using UnityEngine;
using System.Collections;
using UnityEditor;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Collections.Generic;

public class FindReferences
{
      [MenuItem("Assets/资源引用查找", false, 11)]
      static private void Find()
      {
            EditorSettings.serializationMode = SerializationMode.ForceText;
            string path = AssetDatabase.GetAssetPath(Selection.activeObject);
            if (!string.IsNullOrEmpty(path))
            {
                  string guid = AssetDatabase.AssetPathToGUID(path);
                  List<string> withoutExtensions = new List<string>() {".prefab"};
                  string[] files = Directory.GetFiles(Application.dataPath + "/UI/Prefab", "*.*", 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("匹配结束");
                        }

                  };
            }
      }
}

 

posted @ 2023-02-20 16:17  邪心鳞宝  阅读(323)  评论(0编辑  收藏  举报