[转]unity如何查找某个脚本挂在了哪些物体上

在开发中往往会遇到一个问题:不知道整个场景中究竟有哪些物体挂载了某一个脚本。如果挨个查找太麻烦了,下面有一种方法可以快速找到解决这个问题

在unity的Window里有一项Editor tests runner 选择这个会出现一个窗口:如下图:

然后点击创建脚本会有脚本自动创建在project里的Editor下。之后我们要写两个脚本(如下图)

这两个脚本代码,一个是用来盛放要被找的那些物体另个是盛放你要来查找被物体挂载的脚本:

盛放物体的代码:

 
  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. }


盛放脚本的代码:

 
  1. using UnityEngine;

  2. using System.Collections;

  3. using System.Collections.Generic;

  4. using UnityEditor;

  5.  
  6. /////////////////////////////////////////////////////////////////////////////

  7. //查找节点及所有子节点中,是否有指定的脚本组件

  8. /////////////////////////////////////////////////////////////////////////////

  9. public class MonoFinder : EditorWindow

  10. {

  11. Transform root = null;

  12. MonoScript scriptObj = null;

  13. int loopCount = 0;

  14.  
  15. List<Transform> results = new List<Transform>();

  16.  
  17. [MenuItem("Level4/Finder/MonoFinder")]

  18. static void Init()

  19. {

  20. EditorWindow.GetWindow(typeof(MonoFinder));

  21. }

  22.  
  23. void OnGUI()

  24. {

  25. GUILayout.Label("节点:");

  26. root = (Transform)EditorGUILayout.ObjectField(root, typeof(Transform), true);

  27. GUILayout.Label("脚本类型:");

  28. scriptObj = (MonoScript)EditorGUILayout.ObjectField(scriptObj, typeof(MonoScript), true);

  29. if (GUILayout.Button("Find"))

  30. {

  31. results.Clear();

  32. loopCount = 0;

  33. Debug.Log("开始查找.");

  34. FindScript(root);

  35. }

  36. if (results.Count > 0)

  37. {

  38. foreach (Transform t in results)

  39. {

  40. EditorGUILayout.ObjectField(t, typeof(Transform), false);

  41. }

  42. }

  43. else

  44. {

  45. GUILayout.Label("无数据");

  46. }

  47. }

  48.  
  49. void FindScript(Transform root)

  50. {

  51. if (root != null && scriptObj != null)

  52. {

  53. loopCount++;

  54. Debug.Log(".." + loopCount + ":" + root.gameObject.name);

  55. if (root.GetComponent(scriptObj.GetClass()) != null)

  56. {

  57. results.Add(root);

  58. }

  59. foreach (Transform t in root)

  60. {

  61. FindScript(t);

  62. }

  63. }

  64. }

  65. }

有了这两个脚本,会发现unity的菜单里会多出一个level4(如图)的选项,然后点击它,会出现一个弹窗。上面那个节点(如图)就是盛放物体的,下面那个脚本类型(如图)就是放脚本的。

例如下面的案例中,我要查找一个名字叫AsyncImageDownloader的脚本在panoramic这个物体里有多少被挂载了。直接把对应的东西拖进去,然后点击find就会发现在userhead_portrait这个子物体里有这个脚本。


 

这种方法可以找到所有父物体下的子物体中所有的挂载。它会遍历整个父物体中个的子物体

从下图中可以看出在panoramic这个父物体中有575个子物体,它们都被查找了一遍。可见刚才查到只有userhead_portrait上挂载了刚才要找的脚本。

猜你喜欢

转载自blog.csdn.net/u014732824/article/details/82588873