Unity 删除丢失Missing脚本组件


using UnityEngine;
using UnityEditor;
 
public class DeleteMissingScripts
{
    [MenuItem("MyTools/Delete Missing Scripts")]
    static void CleanupMissingScript()
    {
        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]);
                var prop = serializedObject.FindProperty("m_Component");
                r = 0;
 
                for (j = 0; j < components.Length; j++)
                {
                    if (components[j] == null)
                    {
                        prop.DeleteArrayElementAtIndex(j - r);
                        r++;
                    }
                }
 
                serializedObject.ApplyModifiedProperties();
            }
        }
    }
}

原文链接

你可能感兴趣的:(Unity 删除丢失Missing脚本组件)