300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > Unity-Find-Script-References 查找脚本的引用

Unity-Find-Script-References 查找脚本的引用

时间:2019-04-27 06:47:19

相关推荐

Unity-Find-Script-References 查找脚本的引用

背景:

有时候我们需要找出项目中所有的引用到某个脚本的地方(比如Prefabs/Scene GameObjects等)。当项目比较大时,手工寻找还是非常费时费力的事情。本文尝试通过插件自动搜索。

分析:

基本的思路是:首先筛选出项目中全部Prefab,加载每个Prefab并判断是否有挂载目标脚本,然后载入每个场景,判断场景中每个物体是否有挂载目标脚本,最后列出结果。

实现:

1,在右键菜单项中添加菜单:

新建一个类,命名为 FindScriptRef ,并继承自 EditorWindow 。添加如下方法:

[MenuItem(“Assets/Find All Reference”)]public static void ShowWindow(){//Show existing window instance. If one doesn’t exist, make one. EditorWindow.GetWindow(typeof(FindScriptRef));}

这段代码会在菜单中添加一个名为“Find All Reference”的菜单项。选中菜单项会打开一个FindScriptRef窗口实例。当然此时窗口中没有任何内容。

2,窗口基本显示逻辑:

void OnGUI(){if (Selection.activeObject == null) {GUILayout.Label(“select a script file from Project Window.”); return; }

//判断选中项是否为脚本var name = Selection.activeObject.name;System.Reflection.Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly();var dict = System.IO.Path.GetDirectoryName(assembly.Location);assembly = System.Reflection.Assembly.LoadFile(System.bine(dict, "Assembly-CSharp.dll"));var selectType = assembly.GetType(name);if (string.IsNullOrEmpty(name) || selectType == null){GUILayout.Label("select a script file from Project Window.");return;}GUILayout.BeginVertical();GUILayout.BeginHorizontal();//列出脚本名称和“Find”按钮GUILayout.Label(name);bool click = GUILayout.Button("Find");GUILayout.EndHorizontal();GUILayout.Space(10);//列出搜索结果if (findResult != null && findResult.Count > 0){GUILayout.BeginScrollView(Vector2.zero, GUIStyle.none);foreach (string path in findResult){GUILayout.Label(path);}GUILayout.EndScrollView();}if (click){Find(selectType);}GUILayout.EndVertical();

}

然后,实现Find方法,搜索指定Type的全部引用:

void Find(System.Type type){

//step 1:find ref in assets//filter all GameObject from assets(so-called 'Prefab')var guids = AssetDatabase.FindAssets("t:GameObject");findResult = new List<string>();var tp = typeof(GameObject);foreach (var guid in guids){var path = AssetDatabase.GUIDToAssetPath(guid);//load Prefabvar obj = AssetDatabase.LoadAssetAtPath(path, tp) as GameObject;//check whether prefab contains script with type 'type'if (obj != null){var cmp = obj.GetComponent(type);if (cmp == null){cmp = obj.GetComponentInChildren(type);}if (cmp != null){findResult.Add(path);}}}//step 2: find ref in scenes//save current scenestring curScene = EditorApplication.currentScene;EditorApplication.SaveScene();//find all scenes from dataPathstring[] scenes = Directory.GetFiles(Application.dataPath, "*.unity", SearchOption.AllDirectories);//iterates all scenes foreach (var scene in scenes){EditorApplication.OpenScene(scene);//iterates all gameObjectsforeach (GameObject obj in FindObjectsOfType<GameObject>()){var cmp = obj.GetComponent(type);if (cmp == null){cmp = obj.GetComponentInChildren(type);}if (cmp != null){findResult.Add(scene.Substring(Application.dataPath.Length) + "Assets:" + obj.name);}}}//reopen current sceneEditorApplication.OpenScene(curScene);Debug.Log ("finish");

}

此时切换到Unity,在Project窗口选中一个scrpit,右键选择『Find All Reference』,在打开的窗口选择『Find』按钮,即可看到下面列出了所有引用了这个脚本的位置(如果项目过于庞大,可能需要等待一会儿)。

代码下载:[FindScriptRef.cs]({{ site.url }}/files/FindScriptRef.cs “FindScriptRef.cs”)

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。