美文网首页unity3D技术分享
unity-Hierarchy视图选中预制件批量替换组件

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

作者: 好怕怕 | 来源:发表于2018-11-09 21:50 被阅读11次

在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<Shadow>();
        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视图选中预制件批量替换组件

    本文链接:https://www.haomeiwen.com/subject/fxraxqtx.html