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  }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM