Skip to content

Instantly share code, notes, and snippets.

@karl-
Last active April 17, 2023 17:20
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save karl-/8a7fcdbcf1e2e906d3b4 to your computer and use it in GitHub Desktop.
Save karl-/8a7fcdbcf1e2e906d3b4 to your computer and use it in GitHub Desktop.
Unity Shift-Duplicate
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;
}
}
@Heislitz
Copy link

Heislitz commented Dec 1, 2022

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

@Heislitz
Copy link

Heislitz commented Dec 1, 2022

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;
}

}

@karl-
Copy link
Author

karl- commented Dec 3, 2022

If you're not using ProBuilder you can remove all of the probuilder imports.

@Heislitz
Copy link

Heislitz commented Dec 3, 2022

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

@2coder
Copy link

2coder commented Feb 22, 2023

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;
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment