| | |
| | | using System.Collections; |
| | | using UnityEditor; |
| | | using UnityEngine.SceneManagement; |
| | | using System.Collections.Generic; |
| | | using System.IO; |
| | | using System.Linq; |
| | | |
| | | public class RemoveMissingScripts : Editor |
| | | public class RemoveMissingScripts : EditorWindow |
| | | { |
| | | Vector2 scrollPos = Vector2.zero; |
| | | List<string> pathList = new List<string>(); |
| | | List<GameObject> missObj = new List<GameObject>(); |
| | | |
| | | private void CleanupMissingScripts() |
| | | { |
| | | foreach (GameObject obj in missObj) |
| | | { |
| | | // var components = obj.GetComponents<Component>(); |
| | | // var serializedObject = new SerializedObject(obj); |
| | | var count = GameObjectUtility.RemoveMonoBehavioursWithMissingScript(obj); |
| | | // if (count > 0) |
| | | Debug.LogFormat("清空空脚本:{0}", count); |
| | | // serializedObject.ApplyModifiedProperties(); |
| | | EditorUtility.SetDirty(obj); |
| | | } |
| | | AssetDatabase.SaveAssets(); |
| | | AssetDatabase.Refresh(); |
| | | } |
| | | |
| | | private void CheckMissingScripts() |
| | | { |
| | | List<string> withoutExtensions = new List<string>() { ".prefab" }; |
| | | string[] files = Directory.GetFiles(Application.dataPath, "*.*", SearchOption.AllDirectories) |
| | | .Where(s => withoutExtensions.Contains(Path.GetExtension(s).ToLower())).ToArray(); |
| | | |
| | | int count = 0; |
| | | foreach (string fileName in files) |
| | | { |
| | | count++; |
| | | EditorUtility.DisplayProgressBar("Processing...", "搜寻所有Prefab....", count / files.Length); |
| | | string fixedFileName = fileName.Replace("\\", "/"); |
| | | int index = fixedFileName.IndexOf("Assets"); |
| | | fixedFileName = fixedFileName.Substring(index); |
| | | pathList.Add(fixedFileName); |
| | | } |
| | | EditorUtility.ClearProgressBar(); |
| | | |
| | | int count2 = 0; |
| | | foreach (string path in pathList) |
| | | { |
| | | count2++; |
| | | EditorUtility.DisplayProgressBar("Processing...", "遍历丢失脚本Prefab中....", count / pathList.Count); |
| | | GameObject obj = (GameObject)AssetDatabase.LoadAssetAtPath(path, typeof(GameObject)); |
| | | getAllChild(obj.transform); |
| | | } |
| | | EditorUtility.ClearProgressBar(); |
| | | } |
| | | |
| | | void getAllChild(Transform tf) |
| | | { |
| | | foreach (Transform child in tf) |
| | | { |
| | | var components = child.GetComponents<Component>(); |
| | | for (int j = 0; j < components.Length; j++) |
| | | { |
| | | if (components[j] == null) |
| | | { |
| | | missObj.Add(child.gameObject); |
| | | continue; |
| | | } |
| | | } |
| | | getAllChild(child); |
| | | } |
| | | } |
| | | |
| | | void OnGUI() |
| | | { |
| | | scrollPos = EditorGUILayout.BeginScrollView(scrollPos); |
| | | |
| | | if (GUILayout.Button("[查找所有Prefab上的Miss脚本]", GUILayout.Width(300))) |
| | | { |
| | | pathList = new List<string>(); |
| | | missObj = new List<GameObject>(); |
| | | CheckMissingScripts(); |
| | | } |
| | | |
| | | EditorGUILayout.Space(); |
| | | |
| | | if (GUILayout.Button("[删除所有Prefab上的Miss脚本]", GUILayout.Width(300))) |
| | | { |
| | | CleanupMissingScripts(); |
| | | } |
| | | |
| | | EditorGUILayout.Space(); |
| | | EditorGUILayout.Space(); |
| | | |
| | | foreach (GameObject obj in missObj) |
| | | { |
| | | EditorGUILayout.ObjectField(obj, typeof(GameObject), false); |
| | | } |
| | | |
| | | EditorGUILayout.EndScrollView(); |
| | | } |
| | | |
| | | [MenuItem("程序/移除丢失的脚本")] |
| | | public static void RemoveMissingScript() |
| | | { |
| | | var gos = GameObject.FindObjectsOfType<GameObject>(); |
| | | foreach (var item in gos) |
| | | { |
| | | SerializedObject so = new SerializedObject(item); |
| | | var soProperties = so.FindProperty("m_Component"); |
| | | var components = item.GetComponents<Component>(); |
| | | int propertyIndex = 0; |
| | | foreach (var c in components) |
| | | { |
| | | if (c == null) |
| | | { |
| | | soProperties.DeleteArrayElementAtIndex(propertyIndex); |
| | | } |
| | | ++propertyIndex; |
| | | } |
| | | so.ApplyModifiedProperties(); |
| | | } |
| | | |
| | | AssetDatabase.Refresh(); |
| | | Debug.Log("清理完成!"); |
| | | EditorWindow.GetWindow<RemoveMissingScripts>("移除丢失的脚本").Show(); |
| | | } |
| | | |
| | | } |