Unity查找Editor下Project视图中特定的资源

 1 [MenuItem("Tools/Check Text Count")]
 2 public static void CheckText ()
 3 {
 4     //查找指定路径下指定类型的所有资源,返回的是资源GUID
 5     string[] guids = AssetDatabase.FindAssets ("t:GameObject", new string[] { "Assets/Resources/UI" });
 6     //从GUID获得资源所在路径
 7     List<string> paths = new List<string> ();
 8     guids.ToList ().ForEach (m => paths.Add (AssetDatabase.GUIDToAssetPath (m)));
 9     //从路径获得该资源
10     List<GameObject> objs = new List<GameObject> ();
11     paths.ForEach (p => objs.Add (AssetDatabase.LoadAssetAtPath (p, typeof (GameObject)) as GameObject));
12     //下面就可以对该资源做任何你想要的操作了,如查找已丢失的脚本、检查赋值命名等,这里查找所有的Text组件个数
13     List<Text> texts = new List<Text> ();
14     objs.ForEach (o => texts.AddRange (o.GetComponentsInChildren<Text> (true)));
15     Debug.Log ("Text count:" + texts.Count);
16 }

 查找到你想要处理的资源后可以进行各种自定操作了,可参考以下博主的示例:

http://www.xuanyusong.com/archives/3727

其中删除missing的脚本很有用哦,单独Mark一下:

 1 [MenuItem("Edit/Cleanup Missing Scripts")]
 2  static void CleanupMissingScripts ()
 3  {
 4      for(int i = 0; i < Selection.gameObjects.Length; i++)
 5      {
 6          var gameObject = Selection.gameObjects[i];
 7          
 8          // We must use the GetComponents array to actually detect missing components
 9          var components = gameObject.GetComponents<Component>();
10          
11          // Create a serialized object so that we can edit the component list
12          var serializedObject = new SerializedObject(gameObject);
13          // Find the component list property
14          var prop = serializedObject.FindProperty("m_Component");
15          
16          // Track how many components we've removed
17          int r = 0;
18          
19          // Iterate over all components
20          for(int j = 0; j < components.Length; j++)
21          {
22              // Check if the ref is null
23              if(components[j] == null)
24              {
25                  // If so, remove from the serialized component array
26                  prop.DeleteArrayElementAtIndex(j-r);
27                  // Increment removed count
28                  r++;
29              }
30          }
31          
32          // Apply our changes to the game object
33          serializedObject.ApplyModifiedProperties();
34          //这一行一定要加!!!
35          EditorUtility.SetDirty(gameObject);
36      }
37  }

 

posted @ 2016-06-07 11:45  桫椤  阅读(4698)  评论(1编辑  收藏  举报