2020-06-29【UnityEditor】2019批量删除丢

2020-06-29  本文已影响0人  持刀的要迟到了

关键方法:

GameObjectUtility.RemoveMonoBehavioursWithMissingScript

由于unity版本更新,网上找的代码不能用了。
但是使用这个方法可以。
代码:

using UnityEngine;
using UnityEditor;

public class RemoveMissingScriptsRecursively : EditorWindow
{
    [MenuItem("Tools/RemoveMissingScriptsRecursively")]
    public static void ShowWindow()
    {
        EditorWindow.GetWindow(typeof(RemoveMissingScriptsRecursively));
    }

    public void OnGUI()
    {
        if (GUILayout.Button("Remove Missing Scripts in selected GameObjects"))
        {
            RemoveInSelected();
        }
    }
    private static void RemoveInSelected()
    {
        GameObject[] go = Selection.gameObjects;
        foreach (GameObject g in go)
        {
            RemoveRecursively(g);
        }
    }

    private static void RemoveRecursively(GameObject g)
    {
        GameObjectUtility.RemoveMonoBehavioursWithMissingScript(g);

        foreach (Transform childT in g.transform)
        {
            RemoveRecursively(childT.gameObject);
        }
    }
}
上一篇下一篇

猜你喜欢

热点阅读