unity-Hierarchy视图选中预制件批量替换组件

在Hierarchy选中指定的物体,直接应用或者inspecor面板单机右键移除

using UnityEditor;
using UnityEngine;
using UnityEngine.UI;

public class CommonMenuEditor : EditorWindow
{

    [InitializeOnLoadMethod]
    static void Start()
    {
        PrefabUtility.prefabInstanceUpdated += OnRemove;
    }
    static public void OnRemove(GameObject obj)
    {
        Remove();
    }

    // 预制件鼠标右键移除
    [MenuItem("CONTEXT/Transform/Remove")]
    static public void Remove()
    {
        GameObject source = PrefabUtility.GetPrefabParent(Selection.activeGameObject) as GameObject;
        if (source == null) return;
        string prefabPath = AssetDatabase.GetAssetPath(source).ToLower();

        if (prefabPath.EndsWith(".prefab") == false) return;

        var items = Selection.activeGameObject.GetComponentsInChildren();
        if (items.Length == 0)
        {
            return;
        }
        foreach (var item in items)
        {
            GameObject.DestroyImmediate(item, true);
        }

        GameObject go = Selection.activeGameObject;
        PrefabUtility.ReplacePrefab(Selection.activeGameObject, source, ReplacePrefabOptions.ConnectToPrefab | ReplacePrefabOptions.ReplaceNameBased);
        EditorApplication.delayCall = delegate
        {
            Selection.activeGameObject = go;
        };
        AssetDatabase.SaveAssets();
    }
}

你可能感兴趣的:(unity-Hierarchy视图选中预制件批量替换组件)