Unity 编辑器插件-快速删除组件

实现删除所选物体及子物体中指定组件。自动apply

同时考虑一个物体上挂在多个相同组件的情况

#if UNITY_EDITOR_WIN    

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

public class QuickDelectComponent : ScriptableWizard
{
    public string ComponentName;
    [MenuItem("Tools/DelectComponent")]
    public static void SelectAllOfTypeMenuItem()
    {
        ScriptableWizard.DisplayWizard("Select objects of type...", typeof(QuickDelectComponent), "确定", "应用");

    }

    private void OnWizardUpdate()
    {
        helpString = "Set the string of the Component tpe you want to Delect";
    }

    /// <summary>
    /// 执行 不会关闭窗口
    /// </summary>
    private void OnWizardOtherButton()
    {
        Transform[] transforms = Selection.GetTransforms(SelectionMode.Deep);
        List<Transform> hasTypeComponent = new List<Transform>();
        foreach (var item in transforms)
        {
            if (item.GetComponent(ComponentName) != null)
            {
                DelectComponent(item);
            }
        }

        // 获取预制体资源
        var prefabAsset = UnityEditor.PrefabUtility.GetCorrespondingObjectFromOriginalSource(Selection.activeObject);
        string path = UnityEditor.AssetDatabase.GetAssetPath(prefabAsset);
        //Apply
        PrefabUtility.SaveAsPrefabAsset(Selection.activeGameObject, path);
    }
    /// <summary>
    /// 执行 会关闭窗口
    /// </summary>
    private void OnWizardCreate()
    {
        Transform[] transforms = Selection.GetTransforms(SelectionMode.Deep);
        foreach (var item in transforms)
        {
            if (item.GetComponent(ComponentName) != null)
            {
                DelectComponent(item);
            }
        }
        // 获取预制体资源
        var prefabAsset = UnityEditor.PrefabUtility.GetCorrespondingObjectFromOriginalSource(Selection.activeObject);
        string path = UnityEditor.AssetDatabase.GetAssetPath(prefabAsset);
        //Apply
        PrefabUtility.SaveAsPrefabAsset(Selection.activeGameObject, path);
    }
    void DelectComponent(Transform _transform)
    {
        Object.DestroyImmediate(_transform.GetComponent(ComponentName));
        if (_transform.GetComponent(ComponentName) != null)
        {
            DelectComponent(_transform);
        }
    }
}
#endif

猜你喜欢

转载自blog.csdn.net/qq_34421469/article/details/126746754
今日推荐