- Home /
How do I properly duplicate an object in a editor script?
Hi.
I am writing an editor script that lets you duplicate what you have selected around even easier than with ctrl+d.
But when I use the following code on a barrel I get "barrel(Clone)" as a result and the newly created object is not a prefab.
GameObject go = Object.Instantiate(transform.gameObject, transform.position, transform.rotation) as GameObject;
What I am basically after is the exact code behind the ctrl+d functionality in the Unity editor.
Answer by thylaxene · Mar 29, 2012 at 07:38 AM
Or if you really want recreate the way the Editor duplicates things try this (Unity 3.5).
[MenuItem("Extra/Duplicate Selected")]
public static void DuplicateSelected ()
{
Object prefabRoot = PrefabUtility.GetPrefabParent (Selection.activeGameObject);
if (prefabRoot != null)
PrefabUtility.InstantiatePrefab (prefabRoot);
else
Instantiate (Selection.activeGameObject);
}
thylaxene: I tried this but got the following error: The type or namespace name `$$anonymous$$enuItem' could not be found
Please help me!
But how would you mimic ctrl + d on prefabs in the "Project" view?
lokesh: You need to have using UnityEditor at the top of your script.
Ben: I have no idea.
Figured it out...
AssetDatabase.CopyAsset(oldPath, newPath);
AssetDatabase.Refresh();
While this is correct its not 100% similar to the edit "Edit/Duplicate" If you try to duplicate something from with in the prefab it will find its root and duplicate that.
The Best thing is to invoke the edit window function with the code below:
EditorWindow.focusedWindow.SendEvent (EditorGUIUtility.CommandEvent ("Duplicate"));
is also suggest you focus on the Sceneview first cause you could accidentally duplicate folder and scenes and then you will have something like this.
SceneView.lastActiveSceneView.Focus ();
EditorWindow.focusedWindow.SendEvent (EditorGUIUtility.CommandEvent ("Duplicate"));
Answer by Sbd · Sep 20, 2011 at 09:57 AM
This will do the same as pressing ctrl + d
[MenuItem("Extra/Duplicate Selected Prefab")]
public static void DuplicateSelectedPrefab()
{
Object prefabRoot = EditorUtility.GetPrefabParent(Selection.activeGameObject);
EditorUtility.InstantiatePrefab(prefabRoot);
}
Answer by Anisoropos · Mar 20, 2021 at 08:21 AM
Updating because GetPrefabParent has been deprecated around 2019.1 (plus some extra functionality)
This works on the scene view only - if you want to duplicate in the project (ie. assets) see Ben Blaut's comment
// Map it to Alt + D
[UnityEditor.MenuItem("GaneObject/Duplicate &D", false, 0)]
public static void Duplicate()
{
List<UnityEngine.Object> clones = new List<UnityEngine.Object>();
foreach (GameObject o in Selection.gameObjects)
{
if (!o.transform) continue; // Scene view
GameObject clone;
GameObject prefab = PrefabUtility.GetCorrespondingObjectFromSource(o);
if (prefab != null)
clone = PrefabUtility.InstantiatePrefab(prefab, o.transform.parent) as GameObject;
else
clone = UnityEngine.Object.Instantiate(o, o.transform.parent);
if (clone == null) continue;
// Do any operations you want on to the cloned object (otherwise this function behaves just like Ctrl+D
// <---- CUSTOM LOGIC BEGIN ---->
// ie. If you want to randomize scale / rotation you can do so here
// clone.transform.localScale = ;
// clone.transform.rotation = ;
// <---- CUSTOM LOGIC END ---->
clones.Add(clone);
// Register the duplicated objects to be able to undo
Undo.RegisterCreatedObjectUndo(clone, "Duplicate");
}
if (clones.Count == 0) return;
// Select the duplicated objects
Selection.objects = clones.ToArray();
}
When I duplicated the gameobject that include prefabs - prefabs in newly instantiated copy becoms non-prefabs.
Is there a solution for to keep them as prefabs?
Thank you :)
Don't forget PrefabUtility.SetPropertyModifications(clone, PrefabUtility.GetPropertyModifications(o));
to copy modifications done on the copied prefab instance.
Your answer
Follow this Question
Related Questions
How to reset a component on duplication? 4 Answers
Placing Prefabs in Prefabs. 9 Answers
FindGameObjectsWithTag returns extra prefabs nowhere in the hierarchy 0 Answers
Prefab referenced into Inspector and then Istantiated: Twice in memory? 0 Answers
OnTriggerEnter2D not working on sprite's prefab clones 0 Answers