Skip to content

Instantly share code, notes, and snippets.

@Ivanca
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ivanca/0449d625a158ceec6212 to your computer and use it in GitHub Desktop.
Save Ivanca/0449d625a158ceec6212 to your computer and use it in GitHub Desktop.
Duplicate Selected GameObject To all Scenes (For Unity3D) [New Option In Transform inspector Menu]
using UnityEditor;
using UnityEngine;
using System.Collections;
public class DuplicateToAllScenesMenu : MonoBehaviour {
// Add a menu item called "Double Mass" to a Rigidbody's context menu.
[MenuItem ("CONTEXT/Transform/DuplicateToAllScenes")]
static void DuplicatePlz (MenuCommand command) {
EditorApplication.SaveCurrentSceneIfUserWantsTo ();
string tempfilepath = Random.Range(1, 100) + "temp.unity";
string objectOrigin = EditorApplication.currentScene;
objectOrigin = objectOrigin.Substring (objectOrigin.LastIndexOf ('/') + 1);
Transform currentTransform = (Transform)command.context;
string objectName = currentTransform.name;
currentTransform.parent = null;
foreach (GameObject obj in UnityEngine.Object.FindObjectsOfType(typeof(GameObject)))
{
if (obj && obj.transform.parent == null && obj.transform != currentTransform)
{
DestroyImmediate(obj);
}
}
EditorApplication.SaveScene(tempfilepath, true);
foreach (EditorBuildSettingsScene scene in EditorBuildSettings.scenes) {
string scenepath = scene.path.Substring (scene.path.LastIndexOf ('/') + 1);
Transform parent = null;
if (scenepath.Equals(objectOrigin)) {
continue;
}
EditorApplication.OpenScene(scene.path);
foreach (GameObject gameObj in UnityEngine.Object.FindObjectsOfType(typeof(GameObject)))
{
if(gameObj.name == objectName)
{
parent = gameObj.transform.parent;
DestroyImmediate(gameObj);
}
}
EditorApplication.OpenSceneAdditive(tempfilepath);
GameObject.Find(objectName).transform.parent = parent;
EditorApplication.SaveScene();
}
FileUtil.DeleteFileOrDirectory(tempfilepath);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment