UnityEditor扩展开发删除missing script

本文提供了一段Unity编辑器脚本,用于递归删除GameObject及其子对象上的Missing脚本。代码包括两种方法,一种是通过SerializedObject删除,另一种是使用GameObjectUtility辅助方法。此外,还提供了可选择多个对象进行删除的功能。适用于清理项目中的无效组件,保持项目整洁。
摘要由CSDN通过智能技术生成

一个很常用的方法

却越来越多坑。。。。

    void DeleteMissingComp(GameObject obj)
    {
        //一开始网上抄了这段代码,但是 for j 的倒装一下也是不会的吗??
        var components = obj.GetComponents<Component>();
        SerializedObject so=new SerializedObject(obj);
        var soProperties = so.FindProperty("m_Component");
        for (int j = 0; j < components.Length; j++)
        { 
            if (components[j] == null)
            {
                soProperties.DeleteArrayElementAtIndex(j-r);
                Debug.LogError("清除了物体:"+obj.name +" 的一个missing脚本");
                r++;
            }
        }
    }

最终的代码:

public class DeleteMissingScript
{
    [MenuItem("Tools/删除Missing脚本(不可还原)")]
    public static void StartDelete()
    {
        var obj = Selection.activeGameObject;
        if (obj == null) return;
        // GameObjectUtility.RemoveMonoBehavioursWithMissingScript(obj);
        // //DeleteMissingComp(obj);
        DeleteRecursive(obj, (go) =>
        {

            GameObjectUtility.RemoveMonoBehavioursWithMissingScript(go);
        });
        
    }
    /// <summary>
    /// 第二次,还是想的太简单,这第三次,写的方法,才是对的
    /// </summary>
    /// <param name="obj"></param>
    /// <param name="doDelete"></param>
    static void DeleteRecursive(GameObject obj,System.Action<GameObject> doDelete)
    {
        doDelete(obj);
        
        for (int i = 0; i < obj.transform.childCount; i++)
        {
            DeleteRecursive(obj.transform.GetChild(i).gameObject,doDelete);
        }

    }
    /// <summary>
    /// 后来发现这段代码是从网上国外论坛抄过来的。。。现在没用了;
    /// 论坛来源:https://forum.unity.com/threads/how-to-remove-missing-scripts-in-a-prefab.569842/
    /// </summary>
    /// <param name="obj"></param>
    static void DeleteMissingComp(GameObject obj)
    {
        var components = obj.GetComponents<Component>();
        SerializedObject so=new SerializedObject(obj);
        var soProperties = so.FindProperty("m_Component");
        for (int j =  components.Length-1;j>=0; j--)
        { 
            if (components[j] == null)
            {
                soProperties.DeleteArrayElementAtIndex(j);
            }
        }
    }
}

以上代码请放在Editor目录下

还是可以重新改一下代码的,可以多选,再删除 Missing 

    [MenuItem("Tools/删除Missing脚本(不可还原)")]
    public static void StartDelete()
    {
        //第四次写,可以选择多个obj 然后删除
        var objs = Selection.objects;
        foreach (var obj in objs)
        {
            GameObject gameObject = obj as GameObject;
            if (gameObject != null)
            {
                DeleteRecursive(gameObject, (go) =>
                {
                    GameObjectUtility.RemoveMonoBehavioursWithMissingScript(go);
                });
            }
        }
    }

参考自:

批量删除预制脚本_小菜冻未条-CSDN博客

使用DeleteArrayElementAtIndex方法删除Missing Script报错:It is not allowed to modify the data property的替代方案_weixin_44348529的博客-CSDN博客

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

avi9111

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值