private void DuplicateSelection(GameObject[] selection) { if (selection != null && selection.Length > 0) { Selection.objects = selection; using (new SelectionChangeApplier(this)) Unsupported.DuplicateGameObjectsUsingPasteboard(); } }
/// <summary> /// Overrides the built in selection duplication to maintain sibling order of the selection. But only if at least one of the selection is under a CSG Model. /// </summary> /// <returns><c>true</c>, if our custom duplication took place (and one of the selection was under a CSG Model), <c>false</c> otherwise in which case the duplication event should not be consumed so that Unity will duplicate for us.</returns> public static bool DuplicateSelection() { List <Transform> selectedTransforms = Selection.transforms.ToList(); // Whether any of the selection objects are under a CSG Model bool shouldCustomDuplicationOccur = false; for (int i = 0; i < selectedTransforms.Count; i++) { if (selectedTransforms[i].GetComponentInParent <CSGModel>() != null) { shouldCustomDuplicationOccur = true; } } if (shouldCustomDuplicationOccur) // Some of the objects are under a CSG Model, so peform our special duplication override { // Sort the selected transforms in order of sibling index selectedTransforms.Sort((x, y) => x.GetSiblingIndex().CompareTo(y.GetSiblingIndex())); GameObject[] newObjects = new GameObject[Selection.gameObjects.Length]; // Walk through each selected object in the correct order, duplicating them one by one for (int i = 0; i < selectedTransforms.Count; i++) { // Temporarily set the selection to the single entry Selection.activeGameObject = selectedTransforms[i].gameObject; // Seems to be a bug in Unity where we need to set the objects array too otherwise it won't be set straight away Selection.objects = new Object[] { selectedTransforms[i].gameObject }; // Duplicate the single entry Unsupported.DuplicateGameObjectsUsingPasteboard(); // Cache the new entry, so when we're done we reselect all new objects newObjects[i] = Selection.activeGameObject; // Remove the 'Brush (1)', 'Brush (2)', etc. from the name. newObjects[i].name = Regex.Replace(newObjects[i].name, " \\(\\d+\\)$", ""); // If we are dealing with a brush, properly duplicate the volume type. BrushBase brush = newObjects[i].GetComponent <BrushBase>(); if (brush != null && brush.Volume != null) { brush.Volume = ScriptableObject.Instantiate(brush.Volume); brush.RebuildVolume(); } } // Finished duplicating, select all new objects Selection.objects = newObjects; } // Whether custom duplication took place and whether the Duplicate event should be consumed return(shouldCustomDuplicationOccur); }
public override void ExecuteAction() { Unsupported.DuplicateGameObjectsUsingPasteboard(); var selection = Selection.transforms; var bounds = ObjectUtils.GetBounds(selection); foreach (var s in selection) { var clone = s.gameObject; clone.hideFlags = HideFlags.None; var cloneTransform = clone.transform; var cameraTransform = CameraUtils.GetMainCamera().transform; var viewDirection = cloneTransform.position - cameraTransform.position; cloneTransform.position = cameraTransform.TransformPoint(Vector3.forward * viewDirection.magnitude / this.GetViewerScale()) + cloneTransform.position - bounds.center; this.AddToSpatialHash(clone); } }
private static void Duplicate() { GameObject[] gameObjects = Selection.gameObjects; Object[] newObjects = new Object[gameObjects.Length]; for (int i = 0; i < gameObjects.Length; i++) { GameObject gameObject = gameObjects[i]; int nSiblingIndex = gameObject.transform.GetSiblingIndex() + 1; Selection.activeGameObject = gameObject; Unsupported.DuplicateGameObjectsUsingPasteboard(); GameObject gameObjectClone = Selection.activeGameObject; gameObjectClone.name = gameObject.name; gameObjectClone.transform.SetSiblingIndex(nSiblingIndex); newObjects[i] = gameObjectClone; Undo.RegisterCreatedObjectUndo(gameObjectClone, "Duplicate"); } Selection.objects = newObjects; }
/// <summary> /// Overrides the built in selection duplication to maintain sibling order of the selection. But only if at least one of the selection is under a CSG Model. /// </summary> /// <returns><c>true</c>, if our custom duplication took place (and one of the selection was under a CSG Model), <c>false</c> otherwise in which case the duplication event should not be consumed so that Unity will duplicate for us.</returns> public static bool DuplicateSelection() { List <Transform> selectedTransforms = Selection.transforms.ToList(); // Whether any of the selection objects are under a CSG Model bool shouldCustomDuplicationOccur = false; for (int i = 0; i < selectedTransforms.Count; i++) { if (selectedTransforms[i].GetComponentInParent <CSGModel>() != null) { shouldCustomDuplicationOccur = true; } } if (shouldCustomDuplicationOccur) // Some of the objects are under a CSG Model, so peform our special duplication override { // Sort the selected transforms in order of sibling index selectedTransforms.Sort((x, y) => x.GetSiblingIndex().CompareTo(y.GetSiblingIndex())); GameObject[] newObjects = new GameObject[Selection.gameObjects.Length]; // Walk through each selected object in the correct order, duplicating them one by one for (int i = 0; i < selectedTransforms.Count; i++) { // Temporarily set the selection to the single entry Selection.activeGameObject = selectedTransforms[i].gameObject; // Seems to be a bug in Unity where we need to set the objects array too otherwise it won't be set straight away Selection.objects = new Object[] { selectedTransforms[i].gameObject }; // Duplicate the single entry Unsupported.DuplicateGameObjectsUsingPasteboard(); // Cache the new entry, so when we're done we reselect all new objects newObjects[i] = Selection.activeGameObject; } // Finished duplicating, select all new objects Selection.objects = newObjects; } // Whether custom duplication took place and whether the Duplicate event should be consumed return(shouldCustomDuplicationOccur); }
private static void Duplicate() { //GameObject[] gameObjects = Selection.gameObjects; //Object[] newObjects = new Object[gameObjects.Length]; GameObject[] gameObjects = Selection.gameObjects; if (gameObjects.Length == 0) { return; } bool isTop = false; int nSiblingIndex = 0; if (gameObjects.Length == 1) { isTop = true; nSiblingIndex = Selection.activeTransform.GetSiblingIndex(); } Unsupported.DuplicateGameObjectsUsingPasteboard(); gameObjects = Selection.gameObjects; for (int i = 0; i < gameObjects.Length; i++) { GameObject gameObject = gameObjects[i]; string strName = gameObject.name; if (strName.EndsWith(")")) { gameObject.name = strName.Remove(strName.Length - 4); } Undo.RegisterCreatedObjectUndo(gameObject, "Duplicate"); } if (isTop) { Selection.activeTransform.SetSiblingIndex(nSiblingIndex + 1); } }
public static void Duplicate(GameObject[] targets) { Validate(targets); Selection.objects = targets; Unsupported.DuplicateGameObjectsUsingPasteboard(); }