unity 查找资源被哪里引用了借鉴篇

本文介绍了一个Unity资源查找脚本,能够批量查找项目中指定资源的所有引用,并提供了详细的代码实现。但缺少忽略特定文件夹及直接选中文件夹进行查找的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

方法大量借鉴雨松大神的文章,

附上链接表示敬意

http://www.xuanyusong.com/archives/4207

好处是自己添加了可以多选操作

不好的是没有“”设置某些文件夹忽略“”的功能,那样可以节省不少时间

而且不能“”选中文件夹查找“,那样有些时候更方便

代码如下:


 
  1. using UnityEngine;
  2. using System.Collections;
  3. using UnityEditor;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Text.RegularExpressions;
  8. using System.Collections.Generic;
  9. using NUnit.Framework;
  10. public class FindReferences
  11. {
  12. [ MenuItem("Assets/Find References", false, 10)]
  13. static private void Find()
  14. {
  15. EditorSettings.serializationMode = SerializationMode.ForceText;
  16. Object[] SelectedAsset = Selection.GetFiltered( typeof(Object), SelectionMode.Assets|SelectionMode.ExcludePrefab );
  17. //此处添加需要命名的资源后缀名,注意大小写。
  18. string[] Filtersuffix = new string[] { ".prefab", ".mat", ".dds", ".png", ".jpg" , ".shader" , ".csv", ".wav", ".mp3"};
  19. if (SelectedAsset.Length == 0) return;
  20. foreach (Object tmpFolder in SelectedAsset) {
  21. string path = AssetDatabase.GetAssetPath(tmpFolder);
  22. if (! string.IsNullOrEmpty (path)) {
  23. string guid = AssetDatabase.AssetPathToGUID (path);
  24. List< string> withoutExtensions = new List< string> (){ ".prefab", ".unity", ".mat", ".asset" };
  25. string[] files = Directory.GetFiles (Application.dataPath, "*.*", SearchOption.AllDirectories)
  26. .Where (s => withoutExtensions.Contains (Path.GetExtension (s).ToLower ())).ToArray ();
  27. int num = 0;
  28. for ( var i = 0; i < files.Length; ++i)
  29. {
  30. string file = files [i];
  31. //显示进度条
  32. EditorUtility.DisplayProgressBar( "匹配资源", "正在匹配资源中...", 1.0f * i / files.Length);
  33. if (Regex.IsMatch (File.ReadAllText (file), guid)) {
  34. Debug.Log (file, AssetDatabase.LoadAssetAtPath<Object> (GetRelativeAssetsPath (file)));
  35. num++;
  36. }
  37. }
  38. if (num == 0) {
  39. Debug.LogError (tmpFolder.name + " 匹配到" + num + "个",tmpFolder) ;
  40. } else if (num == 1) {
  41. Debug.Log (tmpFolder.name + " 匹配到" + num + "个",tmpFolder) ;
  42. } else {
  43. Debug.LogWarning (tmpFolder.name + " 匹配到" + num + "个",tmpFolder) ;
  44. }
  45. num = 0;
  46. // int startIndex = 0;
  47. // EditorApplication.update = delegate() {
  48. // string file = files [startIndex];
  49. //
  50. // bool isCancel = EditorUtility.DisplayCancelableProgressBar ("匹配资源中", file, (float)startIndex / (float)files.Length);
  51. //
  52. // if (Regex.IsMatch (File.ReadAllText (file), guid)) {
  53. // Debug.Log (file, AssetDatabase.LoadAssetAtPath<Object> (GetRelativeAssetsPath (file)));
  54. // }
  55. //
  56. // startIndex++;
  57. // if (isCancel || startIndex >= files.Length) {
  58. //
  59. // EditorApplication.update = null;
  60. // startIndex = 0;
  61. // Debug.Log ("匹配结束" + tmpFolder.name);
  62. // }
  63. //
  64. // };
  65. }
  66. }
  67. EditorUtility.ClearProgressBar ();
  68. }
  69. [ MenuItem("Assets/Find References", true)]
  70. static private bool VFind()
  71. {
  72. string path = AssetDatabase.GetAssetPath(Selection.activeObject);
  73. return (! string.IsNullOrEmpty(path));
  74. }
  75. static private string GetRelativeAssetsPath(string path)
  76. {
  77. return "Assets" + Path.GetFullPath(path).Replace(Path.GetFullPath(Application.dataPath), "").Replace( '\\', '/');
  78. }
  79. }

有时间再完善吧,现在的也够用了。

最近看到个插件

Unity资源断舍离(资源清理重复以及引用被引用查找)

地址:https//blog.youkuaiyun.com/akof1314/article/details/83589111

实现的很好

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值