using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEditor; using System.IO; using System; public class DeleteMissingScripts : EditorWindow { [MenuItem("Tools/移除丢失的脚本")] public static void RemoveMissingScript() { GameObject[] pAllObjects = (GameObject[])Resources.FindObjectsOfTypeAll(typeof(GameObject)); int r; int j; for (int i = 0; i < pAllObjects.Length; i++) { if (pAllObjects[i].hideFlags == HideFlags.None)//HideFlags.None 获取Hierarchy面板所有Object { var components = pAllObjects[i].GetComponents(); var serializedObject = new SerializedObject(pAllObjects[i]); // 将object序列化 var prop = serializedObject.FindProperty("m_Component"); //得到object的Component序列化数组 r = 0; for (j = 0; j < components.Length; j++) { if (components[j] == null) { prop.DeleteArrayElementAtIndex(j - r); r++; } } serializedObject.ApplyModifiedProperties(); } } } [MenuItem("Tools/添加缺失克隆类型")] public static void AddCloneTypes() { Transform[] obj = Selection.GetTransforms(SelectionMode.Deep); for (int i = 0; i < obj.Length; i++) { if (obj[i].GetComponent()) { var types = obj[i].GetComponent().CloneableTypes; } } } [MenuItem("Tools/添加克隆类型帮助类(非油罐类型)")] public static void AddCloneTypesHelper() { Transform[] obj = Selection.GetTransforms(SelectionMode.Deep); for (int i = 0; i < obj.Length; i++) { } } [MenuItem("Tools/检查是否有没有添加的克隆类型")] public static void CheckEmptyCloneTypes() { Transform[] obj = Selection.GetTransforms(SelectionMode.Deep); for (int i = 0; i < obj.Length; i++) { } } private static bool FindShiNei(Transform p) { if (p.name.ToLower() == "shinei") { return true; } else { if (p.parent != null) { return FindShiNei(p.parent); } else { return false; } } } [MenuItem("MyTools/LgsTools/智能检测/Remove Missing-MonoBehavior Component")] static public void RemoveMissComponent() { //此处地址为根地址,代码会从这个地址的文件夹下开始遍历里面的所有文件夹的所有预置体以及它的子物体 string path = Application.dataPath + "/17GuiGangWYGC"; string[] getFoder = Directory.GetDirectories(path); searchFile(getFoder); } //递归遍历文件夹 static public void searchFile(string[] path) { foreach (string item in path) { string[] getFile = Directory.GetFiles(item); foreach (string file in getFile) { FileInfo fileInfo = new FileInfo(file); if (fileInfo.Extension.Equals(".Prefab", StringComparison.CurrentCultureIgnoreCase)) { CheckMissMonoBehavior(file); } } string[] getFoder = Directory.GetDirectories(item); searchFile(getFoder); } } /// /// 删除一个Prefab上的空脚本 /// /// prefab路径 例Assets/Resources/FriendInfo.prefab static void CheckMissMonoBehavior(string path) { //先截取路径,使路径从ASSETS开始 int index = path.IndexOf("Assets/", StringComparison.CurrentCultureIgnoreCase); string newPath = path.Substring(index); GameObject obj = AssetDatabase.LoadAssetAtPath(newPath, typeof(GameObject)) as GameObject; //实例化物体 GameObject go = PrefabUtility.InstantiatePrefab(obj) as GameObject; //递归删除 searchChild(go); // 将数据替换到asset PrefabUtility.SaveAsPrefabAsset(go, newPath); go.hideFlags = HideFlags.HideAndDontSave; //删除掉实例化的对象 DestroyImmediate(go); } //递归物体的子物体 static public void searchChild(GameObject gameObject) { int number = GameObjectUtility.RemoveMonoBehavioursWithMissingScript(gameObject); if (gameObject.transform.childCount > 0) { for (int i = 0; i < gameObject.transform.childCount; i++) { searchChild(gameObject.transform.GetChild(i).gameObject); } } } }