Skip to content

Commit 1eec32e

Browse files
author
Rene Damm
authored
FIX: Duplicating multiple items in action editor creating too many duplicates (#908).
1 parent 790f6cf commit 1eec32e

File tree

2 files changed

+35
-8
lines changed

2 files changed

+35
-8
lines changed

Packages/com.unity.inputsystem/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ however, it has to be formatted properly to pass verification tests.
2626
- Fixed "Create Actions..." on `PlayerInput` creating an asset with an incorrect binding for taps on Touchscreens. \
2727
__NOTE: If you have already created an .inputactions asset with this mechanism, update "tap [Touchscreen]" to "Primary Touch/Tap" to fix the problem manually.__
2828
- Fixed `Invoke CSharp Events` when selected in `PlayerInput` not triggering `PlayerInput.onActionTriggered`.
29+
- Fixed duplicating multiple items at the same time in the action editor duplicating them repeatedly.
2930

3031
### Changed
3132

Packages/com.unity.inputsystem/InputSystem/Editor/AssetEditor/InputActionTreeView.cs

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -550,9 +550,7 @@ public void HandleCopyPasteCommandEvent(Event uiEvent)
550550
DeleteDataOfSelectedItems();
551551
break;
552552
case k_DuplicateCommand:
553-
var buffer = new StringBuilder();
554-
CopySelectedItemsTo(buffer);
555-
PasteDataFrom(buffer.ToString());
553+
DuplicateSelection();
556554
break;
557555
case k_DeleteCommand:
558556
DeleteDataOfSelectedItems();
@@ -564,6 +562,38 @@ public void HandleCopyPasteCommandEvent(Event uiEvent)
564562
}
565563
}
566564

565+
private void DuplicateSelection()
566+
{
567+
var buffer = new StringBuilder();
568+
569+
// If we have a multi-selection, we want to perform the duplication as if each item
570+
// was duplicated individually. Meaning we paste each duplicate right after the item
571+
// it was duplicated from. So if, say, an action is selected at the beginning of the
572+
// tree and one is selected from the end of it, we still paste the copies into the
573+
// two separate locations correctly.
574+
//
575+
// Technically, if both parents and children are selected, we're order dependent here
576+
// but not sure we really need to care.
577+
578+
var selection = GetSelection();
579+
ClearSelection();
580+
581+
// Copy-paste each selected item in turn.
582+
var newItemIds = new List<int>();
583+
foreach (var id in selection)
584+
{
585+
SetSelection(new[] { id });
586+
587+
buffer.Length = 0;
588+
CopySelectedItemsTo(buffer);
589+
PasteDataFrom(buffer.ToString());
590+
591+
newItemIds.AddRange(GetSelection());
592+
}
593+
594+
SetSelection(newItemIds);
595+
}
596+
567597
internal const string k_CopyPasteMarker = "INPUTASSET ";
568598
private const string k_StartOfText = "\u0002";
569599
private const string k_StartOfHeading = "\u0001";
@@ -865,11 +895,7 @@ public void BuildContextMenuFor(Type itemType, GenericMenu menu, bool multiSelec
865895
{
866896
menu.AddDisabledItem(s_RenameLabel);
867897
}
868-
menu.AddItem(s_DuplicateLabel, false, () =>
869-
{
870-
CopySelectedItemsToClipboard();
871-
PasteDataFromClipboard();
872-
});
898+
menu.AddItem(s_DuplicateLabel, false, DuplicateSelection);
873899
menu.AddItem(s_DeleteLabel, false, DeleteDataOfSelectedItems);
874900
}
875901

0 commit comments

Comments
 (0)