How to ‘merge’ scene objects from one scene to another?

I’d like to ‘merge’ many objects from one scene to another, like the ‘merge’ operation found in the 3D modeling apps. The objects I want to merge are mostly prefabs. If I parent them to a game object and turn that one into a prefab, I can easily include them in another scene file, but they are losing their prefabs.

So could you please lighten me on what’s the best workflow for this task? (without losing the prefabs)

If anybody has a handy Editor script to pick up all the transforms of all the selected objects, and then create the same prefabs in another scene and paste the transforms back, that would be really cool.
We’d all be really grateful :slight_smile:

Thanks a lot!

An object can only be part of a single prefab, you can not have prefabs in prefabs.

So what you would need to do is either:

  1. Restructure your prefabs
  2. Use package export and import

I saw that nested prefabs don’t work in Unity.

I have like 8 types of houses (each is a prefab), and they are scattered all over the scene.

I’d like to take just the houses and place them in another scene in the exact same positions.

I think importing and exporting packages is for the prefabs in the ‘Project’ view only, right?

So do you know any other methods to accomplish this?

Thanks!

Ok, I made a quick wizard for it.
I guess it would be great if it got added to the wiki, as it could prove really helpful.
Here’s the code:

using UnityEngine;
using UnityEditor;

public class ReplaceSelectionWithPrefab : ScriptableWizard
{
    public GameObject prefab;
    public bool includeChildren = false;
    public bool excludePrefabs = false;
    public bool deleteSelectedObjects = true;
    public bool copyPosition = true;
    public bool copyRotation = true;
    public bool copyScale = true;

    private SelectionMode modePrefs;
   

    [MenuItem("Custom/Replace Selection with Prefab %r")]
    static void DoSet()
    {
        ScriptableWizard.DisplayWizard("Replace Selection with Prefab", typeof(ReplaceSelectionWithPrefab), "Set");

    }

    void OnWizardUpdate()
    {
        helpString = "Duplicate the selected prefab and place it around the scene to the position of the selected objects. Tick Include Children to also place the Prefab at the position of the children of selected objects or tick Exclude Prefabs if you don't want instanced objects (from prefabs) to be changed. Tick DeleteSelectedObjects to delete the original selection after placing the prefab. The new objects are nicely placed in the same hierarchy as the old ones.";
    }


    void OnWizardCreate()
    {

        if (includeChildren || excludePrefabs)

            modePrefs = (SelectionMode.ExcludePrefab | SelectionMode.Editable | SelectionMode.Deep);
        else
            modePrefs = (SelectionMode.Editable);

        Object[] objs = Selection.GetFiltered(typeof(GameObject), modePrefs);

        foreach (GameObject go in objs)
        {

     

            GameObject clone = EditorUtility.InstantiatePrefab(prefab) as GameObject;
            clone.name = prefab.name;
            clone.transform.parent = go.transform.parent;
            if (copyPosition)
            {
                clone.transform.localPosition = go.transform.localPosition;
            }
            if (copyRotation)
            {
                clone.transform.localRotation = go.transform.localRotation;
            }
            if (copyScale)
            {
                clone.transform.localScale = go.transform.localScale;
            }
               

        }
        if (deleteSelectedObjects)
        {
            foreach (GameObject go in objs)
            {
                DestroyImmediate(go.gameObject);
            }
        }

    }
}