-
-
Save karl-/8a7fcdbcf1e2e906d3b4 to your computer and use it in GitHub Desktop.
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; | |
} | |
} |
I tried the following changes, which works quite fine now, but i´m not sure if there´s not something wacky left in it... :-)
using UnityEngine;
using UnityEditor;
using UnityEngine.ProBuilder;
[InitializeOnLoad]
static class ShiftDuplicate
{
static bool m_IsDragging = false;
static ShiftDuplicate()
{
SceneView.duringSceneGui += OnSceneGUI;
}
static void OnSceneGUI(SceneView scn)
{
Event e = Event.current;
if(!m_IsDragging && e.modifiers == EventModifiers.Shift && e.type == EventType.MouseDrag)
{
m_IsDragging = true;
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;
}
}
If you're not using ProBuilder you can remove all of the probuilder imports.
Hi karl-,
Thanks for checking in and, in particular, for offering your script in the first place. Actually, I am using ProBuilder and also Unity Terrain Tools. Can we do an initial request, in the beginning, asking if the user has installed, for example, ProBuilder, and if not, ignore it so no errors occur?
I'm also curious if there is a way, documented somewhere, to exclude the Shift-Click when using the Terrain Tools since, for example, the brushes will create copies for when holding shift is needed in some brushes while working in the terrain...
Kind regards, Marc
I've improved a little bit on the script, as I needed the functioniality to retain the prefab links.
using UnityEditor;
using UnityEngine;
[InitializeOnLoad]
static class ShiftDuplicate
{
private static bool _isDragging;
static ShiftDuplicate()
{
SceneView.duringSceneGui += OnSceneGUI;
}
static void OnSceneGUI(SceneView scn)
{
Event e = Event.current;
if(!_isDragging && e.modifiers == EventModifiers.Shift && e.type == EventType.MouseDrag)
{
_isDragging = true;
scn.SendEvent(EditorGUIUtility.CommandEvent("Duplicate"));
}
if(e.type is EventType.MouseUp or EventType.Ignore)
_isDragging = false;
}
}
Hi, do you have an idea, what needs to be changed to make this script work in recent Unity 2021.3 ?
It throws the following error: Assets\Editor\ShiftDuplicateEditor.cs(6,7): error CS0246: The type or namespace name 'ProBuilder' could not be found (are you missing a using directive or an assembly reference?)
Kind regards, Marc