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 }
分类:
Unity3D
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!