热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

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

1[MenuItem(ToolsCheckTextCount)]2publicstaticvoidCheckText()3{4查找指定路径下指
 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 objs = new List ();
11     paths.ForEach (p => objs.Add (AssetDatabase.LoadAssetAtPath (p, typeof (GameObject)) as GameObject));
12     //下面就可以对该资源做任何你想要的操作了,如查找已丢失的脚本、检查赋值命名等,这里查找所有的Text组件个数
13     List texts = new List ();
14     objs.ForEach (o => texts.AddRange (o.GetComponentsInChildren (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 )
 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();
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 )
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  }

 


推荐阅读
author-avatar
手机用户2602922195
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有