Unity Editor 检查工程Prefab(预设)中的空组件

在我们做项目的过程中 经常会有预设中出现空的脚本

例如:

导致的原因是因为 脚本的丢失

现在我们来做一个检查工程中有空脚本的预设工具

老规矩直接上代码 放到工程就能用

using UnityEngine;
using UnityEditor;
using System.IO;
using System.Collections.Generic;

public class PrefabTool : EditorWindow
{
    [MenuItem("Prefab Tool/Check Missing Scripts")]
    static void CheckMissingScripts()
    {
        List<string> listString = new List<string>();

        CollectFiles(Application.dataPath, listString);

        for (int i = 0; i < listString.Count; i++)
        {
            string Path = listString[i];

            float progressBar = (float)i / listString.Count;

            EditorUtility.DisplayProgressBar("Check Missing Scripts", "The progress of : " + ((int)(progressBar * 100)).ToString() + "%", progressBar);

            if (!Path.EndsWith(".prefab"))//只处理prefab文件
            {
                continue;
            }

            Path = ChangeFilePath(Path);

            AssetImporter tmpAssetImport = AssetImporter.GetAtPath(Path);

            GameObject prefab = AssetDatabase.LoadAssetAtPath<GameObject>(tmpAssetImport.assetPath);

            if (prefab == null)
            {
                Debug.LogError("空的预设 : " + tmpAssetImport.assetPath);

                continue;
            }

            Transform[] transforms = prefab.GetComponentsInChildren<Transform>();
            //获取所有的子节点;

            for (int j = 0; j < transforms.Length; j++)
            {
                GameObject obj = transforms[j].gameObject;

                var components = obj.GetComponents<Component>();
                //获取对象所有的Component组件
                //所有继承MonoBehaviour的脚本都继承Component

                for (int k = 0; k < components.Length; k++)
                {
                    if (components[k] == null)
                    {
                        Debug.LogError("这个预制中有空的脚本 :" + tmpAssetImport.assetPath + " 挂在对象 : " + obj.name + "");
                    }
                }
            }
        }
        EditorUtility.ClearProgressBar();
    }

    //改变路径  
    //这种格式的路径 "C:/Users/XX/Desktop/aaa/New Unity Project/Assetsa.prefab" 改变成 "Assets/a.prefab"
    static string ChangeFilePath(string path)
    {
        path = path.Replace("\", "/");
        path = path.Replace(Application.dataPath + "/", "");
        path = "Assets/" + path;

        return path;
    }

    //迭代获取文件路径;
    static void CollectFiles(string directory, List<string> outfiles)
    {
        string[] files = Directory.GetFiles(directory);

        outfiles.AddRange(files);

        string[] childDirectories = Directory.GetDirectories(directory);

        if (childDirectories != null && childDirectories.Length > 0)
        {
            for (int i = 0; i < childDirectories.Length; i++)
            {
                string dir = childDirectories[i];
                if (string.IsNullOrEmpty(dir)) continue;
                CollectFiles(dir, outfiles);
            }
        }
    }
}

参考 Unity3D研究院编辑器之不实例化Prefab获取删除更新组件(十五) | 雨松MOMO程序研究院

链接:http://www.xuanyusong.com/archives/3727

这篇博客里面那个删除空脚本的方法 我测试 发现有问题 并不能用!

原文地址:https://www.cnblogs.com/zouqiang/p/6881896.html