unity如何查找某个脚本引用在了哪些物体上问题
问题
如平时合作如果遇到合作完成项目或者查看一些奇奇怪怪的项目时,资源过多,不方便查看各个脚本在哪个对象上引用,该怎么办。
解决方法
第一种:
在Hierarchy界面中的搜索栏中输入你想要搜索的脚本全名,即可找到哪些物件用了这个脚本,此方法也可以用来搜索哪些物体上有哪些物件,比如:Rigidbody等。
第二种:
在Unity工程中→选中你需要查看的脚本→鼠标右键→选中“Find References In Scene”→在Hierarchy窗口中查看
在Hierarchy窗口中,上方是添加当前查看的脚本的对象,下方Path是路径,是自下往上读,当然最上方搜索框中也显示选中对象的路径[ref:路径/../..]
第三种:
PS:建议打包导出的时候删除相关代码否则会报错,目前仅供windows系统版本Unity使用
创建脚本 FindMissingScriptsRecursively 和 MonoFinder
这两个脚本代码,一个是用来盛放要被找的那些物体,另一个是盛放你要来查找被物体挂载的脚本
盛放物体的代码:(FindMissingScriptsRecursively类)
1 using UnityEngine; 2 using UnityEditor; 3 public class FindMissingScriptsRecursively : EditorWindow 4 { 5 static int go_count = 0, components_count = 0, missing_count = 0; 6 7 [MenuItem("Window/FindMissingScriptsRecursively")] 8 public static void ShowWindow() 9 { 10 EditorWindow.GetWindow(typeof(FindMissingScriptsRecursively)); 11 } 12 13 public void OnGUI() 14 { 15 if (GUILayout.Button("Find Missing Scripts in selected GameObjects")) 16 { 17 FindInSelected(); 18 } 19 } 20 private static void FindInSelected() 21 { 22 GameObject[] go = Selection.gameObjects; 23 go_count = 0; 24 components_count = 0; 25 missing_count = 0; 26 foreach (GameObject g in go) 27 { 28 FindInGO(g); 29 } 30 Debug.Log(string.Format("Searched {0} GameObjects, {1} components, found {2} missing", go_count, components_count, missing_count)); 31 } 32 33 private static void FindInGO(GameObject g) 34 { 35 go_count++; 36 Component[] components = g.GetComponents<Component>(); 37 for (int i = 0; i < components.Length; i++) 38 { 39 components_count++; 40 if (components[i] == null) 41 { 42 missing_count++; 43 string s = g.name; 44 Transform t = g.transform; 45 while (t.parent != null) 46 { 47 s = t.parent.name + "/" + s; 48 t = t.parent; 49 } 50 Debug.Log(s + " has an empty script attached in position: " + i, g); 51 } 52 } 53 // Now recurse through each child GO (if there are any): 54 foreach (Transform childT in g.transform) 55 { 56 //Debug.Log("Searching " + childT.name + " " ); 57 FindInGO(childT.gameObject); 58 } 59 } 60 }
盛放脚本的代码:(MonoFinder类)
1 using UnityEngine; 2 using System.Collections; 3 using System.Collections.Generic; 4 using UnityEditor; 5 6 //查找节点及所有子节点中,是否有指定的脚本组件 7 public class MonoFinder : EditorWindow 8 { 9 Transform root = null; 10 MonoScript scriptObj = null; 11 int loopCount = 0; 12 13 List<Transform> results = new List<Transform>(); 14 15 [MenuItem("Level4/Finder/MonoFinder")]//路径可变 16 static void Init() 17 { 18 EditorWindow.GetWindow(typeof(MonoFinder)); 19 } 20 21 void OnGUI() 22 { 23 GUILayout.Label("节点:"); 24 root = (Transform)EditorGUILayout.ObjectField(root, typeof(Transform), true); 25 GUILayout.Label("脚本类型:"); 26 scriptObj = (MonoScript)EditorGUILayout.ObjectField(scriptObj, typeof(MonoScript), true); 27 if (GUILayout.Button("Find")) 28 { 29 results.Clear(); 30 loopCount = 0; 31 Debug.Log("开始查找."); 32 FindScript(root); 33 } 34 if (results.Count > 0) 35 { 36 foreach (Transform t in results) 37 { 38 EditorGUILayout.ObjectField(t, typeof(Transform), false); 39 } 40 } 41 else 42 { 43 GUILayout.Label("无数据"); 44 } 45 } 46 47 void FindScript(Transform root) 48 { 49 if (root != null && scriptObj != null) 50 { 51 loopCount++; 52 Debug.Log(".." + loopCount + ":" + root.gameObject.name); 53 if (root.GetComponent(scriptObj.GetClass()) != null) 54 { 55 results.Add(root); 56 } 57 foreach (Transform t in root) 58 { 59 FindScript(t); 60 } 61 } 62 } 63 }
添加完之后,会在Unity上部分菜单栏中,按照路径【Level4→Finder→MonoFinder】和【window→FindMissingScriptsRecursively】,分别找到对应脚本的功能。

浙公网安备 33010602011771号