Last active
January 31, 2023 05:41
-
-
Save JakubNei/2f1b7abc1727c5608519 to your computer and use it in GitHub Desktop.
A simple little editor extension to copy and paste all components
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 | |
*/ | |
using UnityEngine; | |
using UnityEditor; | |
using System.Collections; | |
public class ComponentsCopier : EditorWindow | |
{ | |
static Component[] copiedComponents; | |
[MenuItem("GameObject/Copy all components %&C")] | |
static void Copy() | |
{ | |
copiedComponents = Selection.activeGameObject.GetComponents<Component>(); | |
} | |
[MenuItem("GameObject/Paste all components %&P")] | |
static void Paste() | |
{ | |
foreach (var targetGameObject in Selection.gameObjects) | |
{ | |
if (!targetGameObject || copiedComponents == null) continue; | |
foreach (var copiedComponent in copiedComponents) | |
{ | |
if (!copiedComponent) continue; | |
UnityEditorInternal.ComponentUtility.CopyComponent(copiedComponent); | |
UnityEditorInternal.ComponentUtility.PasteComponentAsNew(targetGameObject); | |
} | |
} | |
} | |
} |
I agree, feel free to upload improved version and link it here.
Hi, is there a demo to show how to use this script in the editor? I have added it to the editor folder in Unity but im not sure how to use it.
Hi, is there a demo to show how to use this script in the editor? I have added it to the editor folder in Unity but im not sure how to use it.
You can read up about MenuItem https://docs.unity3d.com/ScriptReference/MenuItem.html to understand it more
Would be nice to preserve cross-references between components, but that would be probably way harder to implement.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Why
EditorWindow
is here? Also, it's better to checkcopiedComponents == null
first and only then iterate overSelection.gameObjects
.CopyComponent
andPasteComponentAsNew
returnbool
, so it's better to check it either and log warning or error.