Last active
August 29, 2015 14:04
-
-
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]
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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