Unity3D开发之查找面板上某个脚本(包括Missing)

原地址:http://blog.csdn.net/lihandsome/article/details/24265411

有时候我们需要知道某个脚本在场景上面哪里用到,或者那个脚本被删除了但又没有把相关游戏场景的关联东西删掉,那样我们就要一个脚本来查找一下了:

PS:下面两个脚本都要放到assets/Editor下面哦。。

查找missing的脚本:

[csharp] view plaincopy在CODE上查看代码片派生到我的代码片
 
  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. }  


查找某个脚本的脚本:

[csharp] view plaincopy在CODE上查看代码片派生到我的代码片
 
  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.  Transform root = null;  
  11.  MonoScript scriptObj = null;  
  12.  int loopCount = 0;  
  13.    
  14.  List<Transform> results = new List<Transform>();  
  15.    
  16.  [MenuItem("Level4/Finder/MonoFinder")]  
  17.  static void Init(){  
  18.   EditorWindow.GetWindow(typeof(MonoFinder));  
  19.  }  
  20.    
  21.  void OnGUI(){  
  22.   GUILayout.Label("节点:");  
  23.   root = (Transform)EditorGUILayout.ObjectField(root,typeof(Transform),true);  
  24.   GUILayout.Label("脚本类型:");  
  25.   scriptObj = (MonoScript)EditorGUILayout.ObjectField(scriptObj,typeof(MonoScript),true);  
  26.   if(GUILayout.Button("Find")){  
  27.    results.Clear();  
  28.    loopCount = 0;  
  29.    Debug.Log("开始查找.");  
  30.    FindScript(root);  
  31.   }  
  32.   if(results.Count > 0){  
  33.    foreach(Transform t in results){  
  34.     EditorGUILayout.ObjectField(t,typeof(Transform),false);  
  35.    }  
  36.   }else{  
  37.    GUILayout.Label("无数据");  
  38.   }  
  39.  }  
  40.    
  41.  void FindScript(Transform root){  
  42.   if(root != null && scriptObj != null){  
  43.    loopCount ++;  
  44.    Debug.Log(".."+loopCount+":"+root.gameObject.name);  
  45.    if( root.GetComponent(scriptObj.GetClass()) != null){  
  46.     results.Add(root);  
  47.    }  
  48.    foreach(Transform t in root){  
  49.     FindScript(t);  
  50.    }  
  51.   }  
  52.  }  
  53. }  



相关的链接:

http://wiki.unity3d.com/index.php?title=FindMissingScripts

http://superherosk123.iteye.com/blog/1632627

原文地址:https://www.cnblogs.com/123ing/p/3851206.html