【小工具】Unity批量删除物体上引用丢失的脚本
创始人
2024-11-13 08:37:17
0
介绍

Unity批量删除物体上引用丢失的脚本

使用场景

当在外部导入资源时会造成物体上挂载的脚本引用丢失,一个个删除比较麻烦,可以借用这个工具快速实现

工具原理
  • 继承EditorWindow绘制一个自定义的编辑器窗口

  • 加载Assets目录下所有预制体

    • 获取其所有子物体的Component组件
    • 如果组件为Null则表示丢失
    • 将丢失的物体加入待清理列表
  • 遍历待清理列表,通过**GameObjectUtility.RemoveMonoBehavioursWithMissingScript(obj);**进行清理;

使用API
  • 高亮显示资源并选中
 EditorGUIUtility.PingObject(selectTarget);  Selection.activeObject = selectTarget; 
  • 获取指定文件夹下指定类型得资源路径
Directory.GetFiles("Assets","*.prefab",SearchOption.AllDirectories); 
  • 显示进度条
if(EditorUtility.DisplayCancelableProgressBar("Title","信息",Rage(0,1))) {       EditorUtility.ClearProgressBar();       break; } 
  • 移除丢失脚本的组件
GameObjectUtility.RemoveMonoBehavioursWithMissingScript(gamoObjList[i]); 
  • 编辑器下加载资源、保存资源
AssetDatabase.LoadAssetAtPath(paths[i]);  AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); 
代码
using System.Collections.Generic; using System.IO; using UnityEditor; using UnityEngine;  namespace Tools.MissingScriptsToolWindowTool {     public class MissingScriptsWindowTool : EditorWindow     {         [MenuItem("Tools/Missing Scripts Window")]         public static void ShowWindow()         {             GetWindow(typeof(MissingScriptsWindowTool),false,"Missing Scripts GameObjects");         }          List missGamoObjList = new List();          private void OnEnable()         {                         SelectMissingGameObjects(ref missGamoObjList);         }          private Vector2 scrollPosition = Vector2.zero;         private void OnGUI()         {             GUILayout.BeginHorizontal();             GUILayout.Box("丢失脚本物体列表:");             if(GUILayout.Button("刷新列表",GUILayout.Width(150)))             {                 SelectMissingGameObjects(ref missGamoObjList);             }             if(GUILayout.Button("清除无效脚本",GUILayout.Width(150)))             {                 RemoveMissingGameObjects(ref missGamoObjList);             }             GUILayout.EndHorizontal();              GUILayout.Space(10);             if(missGamoObjList.Count == 0)             {                 GUILayout.Label("没有丢失脚本的物体");                 return;             }              scrollPosition = GUILayout.BeginScrollView(scrollPosition);              for(int i = 0; i < missGamoObjList.Count; i++)             {                 GUILayout.BeginHorizontal("Box",GUILayout.Height(30));                  GUILayout.Label(i.ToString(),"AssetLabel",GUILayout.Width(40));                 GUILayout.Space(10);                 if(GUILayout.Button(missGamoObjList[i].name,"AnimLeftPaneSeparator"))                 {                     GameObject selectTarget = missGamoObjList[i];                     while(selectTarget.transform.parent != null)                     {                         selectTarget = selectTarget.transform.parent.gameObject;                     }                     EditorGUIUtility.PingObject(selectTarget);                     Selection.activeObject = selectTarget;                 }                  GUILayout.EndHorizontal();                 GUILayout.Space(5);             }              GUILayout.EndScrollView();         }          private static void SelectMissingGameObjects(ref List gameObjList)         {             gameObjList.Clear();              string[] paths = Directory.GetFiles("Assets","*.prefab",SearchOption.AllDirectories);             for(int i = 0; i < paths.Length; i++)             {                                 if(EditorUtility.DisplayCancelableProgressBar("查找GameObject···","数量 : " + i,(float)i / paths.Length))                 {                     EditorUtility.ClearProgressBar();                     break;                 }                  GameObject tempObj = AssetDatabase.LoadAssetAtPath(paths[i]);                 if(tempObj != null)                 {                     Transform[] transArr = tempObj.GetComponentsInChildren();                     for(int j = 0; j < transArr.Length; j++)                     {                         Component[] components = transArr[j].GetComponents();                         for(int k = 0; k < components.Length; k++)                         {                             if(components[k] == null)                             {                                 if(!gameObjList.Contains(transArr[j].gameObject))                                     gameObjList.Add(transArr[j].gameObject);                             }                         }                     }                 }             }             EditorUtility.ClearProgressBar();         }          private static void RemoveMissingGameObjects(ref List gameObjList)         {             if(gameObjList.Count == 0)                 return;              for(int i = 0; i < gameObjList.Count; i++)             {                 GameObjectUtility.RemoveMonoBehavioursWithMissingScript(gameObjList[i]);             }             AssetDatabase.SaveAssets();             AssetDatabase.Refresh();             gameObjList.Clear();         }      }  }  

相关内容

热门资讯

透视脚本!wepoker有没有... 透视脚本!wepoker有没有挂,(WEPOKER)一贯是真的有挂(透视)科技辅助器(有挂介绍)1、...
透视辅助器!wepoker插件... 透视辅助器!wepoker插件程序激活码,(WePoKer)好像真的有挂(透视)辅助是真的(有挂揭秘...
透视手机版!wepoker怎么... 透视手机版!wepoker怎么下载游戏,(wepoker)竟然是真的有挂(透视)开辅助能查到(有挂细...
透视透视脚本!wepoker免... 透视透视脚本!wepoker免费脚本,(WePoKer)原来是真的有挂(透视)透视有(有挂攻略)1....
透视国外版!wepoker透视... 透视国外版!wepoker透视有用吗,(wepoker)确实是真的有挂(透视)可以下载游戏(有挂教程...
透视安装包!wepoker破解... 透视安装包!wepoker破解器,(wepoker)总是是真的有挂(透视)辅助器免费安装(有挂黑科技...
透视辅助工具!wepoker有... 透视辅助工具!wepoker有什么规律,(wepoker)确实有挂(透视)私人局透视教程(有挂解说)...
透视透视脚本!wepoker插... 透视透视脚本!wepoker插件下载,(WEPOKER)果然存在有挂(透视)靠谱(有挂攻略)1、we...
透视透视脚本!wepoker免... 透视透视脚本!wepoker免费钻石,(WEPOKER)确实是真的有挂(透视)代打辅助机器人(有挂插...
透视辅助器!wepoker手机... 透视辅助器!wepoker手机插件,(WEPOKER)果然真的是有挂(透视)有挂(有挂解密)1、we...