Created
February 26, 2021 02:52
-
-
Save frekons/2d002e560e8251e01841e2d83e8bf364 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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 |
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
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 []