Last active
April 17, 2023 17:20
-
-
Save karl-/8a7fcdbcf1e2e906d3b4 to your computer and use it in GitHub Desktop.
Unity Shift-Duplicate
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 UnityEngine; | |
using UnityEditor; | |
using ProBuilder.Core; | |
using ProBuilder.EditorCore; | |
[InitializeOnLoad] | |
static class ShiftDuplicate | |
{ | |
static bool m_IsDragging = false; | |
static ShiftDuplicate() | |
{ | |
SceneView.onSceneGUIDelegate += OnSceneGUI; | |
} | |
static void OnSceneGUI(SceneView scn) | |
{ | |
Event e = Event.current; | |
if(!m_IsDragging && e.modifiers == EventModifiers.Shift && e.type == EventType.MouseDrag) | |
{ | |
m_IsDragging = true; | |
// probuilder-specific | |
if(pb_Editor.instance != null && pb_Editor.instance.editLevel == EditLevel.Geometry) | |
return; | |
Object[] selection = Selection.GetFiltered(typeof(Transform), SelectionMode.TopLevel); | |
if(selection != null && selection.Length > 0) | |
{ | |
Object[] duplicates = new Object[selection.Length]; | |
for(int i = 0, c = selection.Length; i < c; i++) | |
{ | |
duplicates[i] = Object.Instantiate(selection[i]); | |
Undo.RegisterCreatedObjectUndo((duplicates[i] as Transform).gameObject, "Shift Duplicate"); | |
} | |
} | |
} | |
if(e.type == EventType.MouseUp || e.type == EventType.Ignore) | |
m_IsDragging = false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I've improved a little bit on the script, as I needed the functioniality to retain the prefab links.