Search Unity

new feature request, ctrl-D to duplicate a component

Discussion in 'Editor Workflows' started by steveh2112, Feb 1, 2020.

  1. steveh2112

    steveh2112

    Joined:
    Aug 30, 2015
    Posts:
    314
    i know you can copy and paste as new component but when i'm setting up hundreds of colliders, that's a lot of clicking, ctrl-D would be awesome and i think super easy
    thanks
     
  2. Xarbrough

    Xarbrough

    Joined:
    Dec 11, 2014
    Posts:
    1,188
    But how would you tell Unity to not duplicate the Gameobject or Prefab asset when you press CTRL+D? I think that’s hard to do UX-wise.

    If you are working with hundreds of colliders you need a massively improved workflow over copy and paste. Custom tools e.g, via ScriptableWizard. ;)
     
  3. steveh2112

    steveh2112

    Joined:
    Aug 30, 2015
    Posts:
    314
    maybe not a keyboard shortcut but a duplicate component menu options should be super easy to do
     
  4. Xarbrough

    Xarbrough

    Joined:
    Dec 11, 2014
    Posts:
    1,188
    Are you aware of “Copy Component” and “Paste Component as New” when context clicking on components? It may be one additional step compared to simply a duplicate component, but also more helpful in the general case when duplicating the component from one Gameobject to another.
     
  5. steveh2112

    steveh2112

    Joined:
    Aug 30, 2015
    Posts:
    314
    sure, that's my point, after doing it 50+ times, i was thinking, one click per would be better than 2
     
  6. Xarbrough

    Xarbrough

    Joined:
    Dec 11, 2014
    Posts:
    1,188
    Try this:

    Code (CSharp):
    1. using UnityEditor;
    2. using UnityEngine;
    3.  
    4. public static class ComponentUtility
    5. {
    6.     [MenuItem("CONTEXT/Component/Duplicate Component")]
    7.     private static void DuplicateComponent(MenuCommand menuCommand)
    8.     {
    9.         DuplicateComponent(menuCommand.context as Component);
    10.     }
    11.  
    12.     public static void DuplicateComponent(Component component)
    13.     {
    14.         if (component == null)
    15.             return;
    16.  
    17.         var attributes = component.GetType().GetCustomAttributes(true);
    18.         foreach (var attr in attributes)
    19.             Debug.Log(attr);
    20.  
    21.         UnityEditorInternal.ComponentUtility.CopyComponent(component);
    22.         UnityEditorInternal.ComponentUtility.PasteComponentAsNew(component.gameObject);
    23.     }
    24. }
    This will duplicate e.g. a BoxCollider2D on the same GameObject. However, some component types are not allowed multiple times on the same GameObject, e.g. SpriteRenderer. In theses cases, this code snippet does nothing. It would be nice to detect such a case, but Unity doesn't seem to use the DisallowMultipleComponent attribute for internal engine types, so it seems impossible to check.
     
    Deozaan and steveh2112 like this.
  7. steveh2112

    steveh2112

    Joined:
    Aug 30, 2015
    Posts:
    314
    wow, thanks, wasn't expecting someone to show me how to do it.

    will try today
     
  8. transat

    transat

    Joined:
    May 5, 2018
    Posts:
    779
    There are packages in the asset store that allow you to copy multiple components at once from one game object to another. I use SmartCopier. But yes, it would be lovely if UT had this built-in, considering how time consuming it can otherwise be. But depending on what you're trying to do you should be able to select a bunch of game objects and add a collider to all of them at once. And you should also be able to mass edit them by selecting the game objects that have colliders and tweaking a value from the component. This doesn't work for every type of component though.
     
  9. SisusCo

    SisusCo

    Joined:
    Jan 29, 2019
    Posts:
    1,326
    Ctrl+D to copy the selected component is great, and it does feel very intuitive - I know having added the functionality to my own alternative inspector. Also being able to use the delete key to remove the selected component is pretty convenient (although needed less often).

    Keyboard support in general unfortunately isn't that great in the Unity inspector out of the gate, everything feels like it has been designed to be used mainly with the mouse.