Skip to content

Instantly share code, notes, and snippets.

@frekons
Created February 26, 2021 02:52
Show Gist options
  • Star 44 You must be signed in to star a gist
  • Fork 8 You must be signed in to fork a gist
  • Save frekons/2d002e560e8251e01841e2d83e8bf364 to your computer and use it in GitHub Desktop.
Save frekons/2d002e560e8251e01841e2d83e8bf364 to your computer and use it in GitHub Desktop.
/*
A simple little editor extension to copy and paste all components
Help from http://answers.unity3d.com/questions/541045/copy-all-components-from-one-character-to-another.html
license: WTFPL (http://www.wtfpl.net/)
author: aeroson
advise: ChessMax
editor: frekons
*/
#if UNITY_EDITOR
using UnityEngine;
using UnityEditor;
using System.Collections;
public class ComponentsCopier
{
static Component[] copiedComponents;
[MenuItem("GameObject/Copy all components %&C")]
static void Copy()
{
if (UnityEditor.Selection.activeGameObject == null)
return;
copiedComponents = UnityEditor.Selection.activeGameObject.GetComponents<Component>();
}
[MenuItem("GameObject/Paste all components %&P")]
static void Paste()
{
if (copiedComponents == null)
{
Debug.LogError("Nothing is copied!");
return;
}
foreach (var targetGameObject in UnityEditor.Selection.gameObjects)
{
if (!targetGameObject)
continue;
Undo.RegisterCompleteObjectUndo(targetGameObject, targetGameObject.name + ": Paste All Components"); // sadly does not record PasteComponentValues, i guess
foreach (var copiedComponent in copiedComponents)
{
if (!copiedComponent)
continue;
UnityEditorInternal.ComponentUtility.CopyComponent(copiedComponent);
var targetComponent = targetGameObject.GetComponent(copiedComponent.GetType());
if (targetComponent) // if gameObject already contains the component
{
if (UnityEditorInternal.ComponentUtility.PasteComponentValues(targetComponent))
{
Debug.Log("Successfully pasted: " + copiedComponent.GetType());
}
else
{
Debug.LogError("Failed to copy: " + copiedComponent.GetType());
}
}
else // if gameObject does not contain the component
{
if (UnityEditorInternal.ComponentUtility.PasteComponentAsNew(targetGameObject))
{
Debug.Log("Successfully pasted: " + copiedComponent.GetType());
}
else
{
Debug.LogError("Failed to copy: " + copiedComponent.GetType());
}
}
}
}
copiedComponents = null; // to prevent wrong pastes in future
}
}
#endif
@STARasGAMES
Copy link

Lifesaver!
Thanks a lot! 👍

@merttoka
Copy link

This doesn't work when I copy all components in the game view and paste them all into the scene view after stopping the play mode. It logs Successfully pasted in the console for all components but values don't change.

I am using Unity 2021.3.2f1.

@STARasGAMES
Copy link

This doesn't work when I copy all components in the game view and paste them all into the scene view after stopping the play mode. It logs Successfully pasted in the console for all components but values don't change.

I am using Unity 2021.3.2f1.

That's just how this script works.
After exiting PlayMode all copied components are destroyed, so there is nothing to copy from

@Apricosma
Copy link

Might there be some way to use this recursively on every child game object with a same name as the one being copied from?
I.e. mass copier for every single component on a game object and components on child objects within said parent object

@mikech2000
Copy link

mikech2000 commented Nov 4, 2023

Thank you for this.

@JeffersonRodrigues7
Copy link

Thanksssss!!!!!!!!!!!! Very useful.

@alex798
Copy link

alex798 commented Dec 14, 2023

Hello, Great code - made some modification to allow copy multiple components of one type (had problems with colliders) - hope will help others :3

Mod

@KnifeOfDunwall
Copy link

KnifeOfDunwall commented Jan 5, 2024

Hi, @alex798 . Im in the same boat as you in respect to having multiple components of the same type that i need to copy but your script isnt working for me. it tells me targetComponent has no Lenght and cannot be indexed by []

@alex798
Copy link

alex798 commented Jan 5, 2024

Hi, @alex798 . Im in the same boat as you in respect to having multiple components of the same type that i need to copy but your script isnt working for me. it tells me targetComponent has no Lenght and cannot be indexed by []

Dunno how it can have no Lenght - maybe u lost S in the end of GetComponentS? - in this case it will get only one element instead of array with said error.

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