当前位置: 首页 > news >正文

Unity3D研究院之查找资源被哪里引用了

Unity3D研究院之查找资源被哪里引用了

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

Unity提供了一个方法 AssetDatabase.GetDependencies(),但是它只能查找这个资源引用了那些资源。 但是我想要的是查找某个资源被那些资源引用了,这是两种相反的查找公式。 其实网上也有很多这样的插件了,似乎代码量都太多了。昨天晚上加班时脑洞打开,想到了一个简单的方法。通过GUID全局搜索匹配。。几行代码就能搞定~~

如下图所示,右键随便选择一个资源,点击 Find References。

 

然后开始匹配资源。

 

匹配结束后会把匹配到的资源Log出来。以下代码直接放到你的工程里就可以直接用。

C#

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

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/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('\\', '/');

    }

}

 

美中不足的地方就是查找速度了。。其实正则表达式匹配的速度还是很快,慢的地方是File.ReadAllText(file) 如果大家有更好的办法,欢迎推荐~

 

今天我无意发现了一个更快的方法,可惜只能在mac上用。比我上面的方法要快N倍啊,快的丧心病狂啊~欢迎大家用啊~~。。。

https://gist.github.com/jringrose/617d4cba87757591ce28

C#

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

using UnityEngine;

using System.Collections;

using UnityEditor;

using System.Collections.Generic;

 

public class FindProject {

#if UNITY_EDITOR_OSX

[MenuItem("Assets/Find References In Project", false, 2000)]

private static void FindProjectReferences()

{

string appDataPath = Application.dataPath;

string output = "";

string selectedAssetPath = AssetDatabase.GetAssetPath (Selection.activeObject);

List<string> references = new List<string>();

string guid = AssetDatabase.AssetPathToGUID (selectedAssetPath);

var psi = new System.Diagnostics.ProcessStartInfo();

psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Maximized;

psi.FileName = "/usr/bin/mdfind";

psi.Arguments = "-onlyin " + Application.dataPath + " " + guid;

psi.UseShellExecute = false;

psi.RedirectStandardOutput = true;

psi.RedirectStandardError = true;

System.Diagnostics.Process process = new System.Diagnostics.Process();

process.StartInfo = psi;

process.OutputDataReceived += (sender, e) => {

if(string.IsNullOrEmpty(e.Data))

return;

string relativePath = "Assets" + e.Data.Replace(appDataPath, "");

// skip the meta file of whatever we have selected

if(relativePath == selectedAssetPath + ".meta")

return;

references.Add(relativePath);

};

process.ErrorDataReceived += (sender, e) => {

if(string.IsNullOrEmpty(e.Data))

return;

output += "Error: " + e.Data + "\n";

};

process.Start();

process.BeginOutputReadLine();

process.BeginErrorReadLine();

process.WaitForExit(2000);

foreach(var file in references){

output += file + "\n";

Debug.Log(file, AssetDatabase.LoadMainAssetAtPath(file));

}

Debug.LogWarning(references.Count + " references found for object " + Selection.activeObject.name + "\n\n" + output);

}

#endif

 

 

 

 

相关文章:

  • Unity VS 暂挂程序
  • xlua扩展第三方库记录
  • XLua-操作与使用
  • 如何优雅使用Sublime Text3(Sublime设置豆沙绿背景色和自定义主题)
  • Unity 无缘无故提示缺少类型解决方法
  • Eclipse 最底下有垃圾回收按钮
  • activity堆栈式管理
  • XLUA学习笔记之C#和Lua之间的相互调用
  • Lua 元表(Metatable)
  • XLua Lua访问C#中的方法(四)访问枚举
  • Unity中Shader和AssetBundle结合使用的注意事项
  • 解决打包AssetBundle时Shader(材质)丢失问题
  • hader in AssetBundle
  • 八:Unity ShaderLab内存优化
  • Unity5 多场景 打包Assetbundle 以及 Shader Stripping 导致 LightMap 全部丢失的解决方法
  • 「前端」从UglifyJSPlugin强制开启css压缩探究webpack插件运行机制
  • 【159天】尚学堂高琪Java300集视频精华笔记(128)
  • Angular2开发踩坑系列-生产环境编译
  • Babel配置的不完全指南
  • CentOS学习笔记 - 12. Nginx搭建Centos7.5远程repo
  • Java,console输出实时的转向GUI textbox
  • Laravel5.4 Queues队列学习
  • Magento 1.x 中文订单打印乱码
  • Node项目之评分系统(二)- 数据库设计
  • React组件设计模式(一)
  • Solarized Scheme
  • Spring框架之我见(三)——IOC、AOP
  • 服务器之间,相同帐号,实现免密钥登录
  • 简单实现一个textarea自适应高度
  • 开放才能进步!Angular和Wijmo一起走过的日子
  • 实战|智能家居行业移动应用性能分析
  • 小程序上传图片到七牛云(支持多张上传,预览,删除)
  • 用 vue 组件自定义 v-model, 实现一个 Tab 组件。
  • 怎么把视频里的音乐提取出来
  • #单片机(TB6600驱动42步进电机)
  • #微信小程序:微信小程序常见的配置传旨
  • #我与Java虚拟机的故事#连载05:Java虚拟机的修炼之道
  • (1)Map集合 (2)异常机制 (3)File类 (4)I/O流
  • (ros//EnvironmentVariables)ros环境变量
  • (zhuan) 一些RL的文献(及笔记)
  • (ZT) 理解系统底层的概念是多么重要(by趋势科技邹飞)
  • (论文阅读32/100)Flowing convnets for human pose estimation in videos
  • (三)elasticsearch 源码之启动流程分析
  • (五)关系数据库标准语言SQL
  • .360、.halo勒索病毒的最新威胁:如何恢复您的数据?
  • .gitignore文件---让git自动忽略指定文件
  • .NET CORE Aws S3 使用
  • .NET 反射的使用
  • .NET/C# 使窗口永不激活(No Activate 永不获得焦点)
  • .NET开发人员必知的八个网站
  • @Pointcut 使用
  • @ResponseBody
  • @软考考生,这份软考高分攻略你须知道
  • [AS3]URLLoader+URLRequest+JPGEncoder实现BitmapData图片数据保存
  • [BZOJ1008][HNOI2008]越狱