Created
September 11, 2015 06:28
-
-
Save tsubaki/ca08811a3d332939f10d 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
| using UnityEditor; | |
| using UnityEditorInternal; | |
| using UnityEngine; | |
| public class CopyComponents : EditorWindow { | |
| private GameObject originalObject = null; | |
| private GameObject targetObject = null; | |
| [MenuItem("Window/CopyComponent")] | |
| static void Init() | |
| { | |
| EditorWindow winow = EditorWindow.CreateInstance<CopyComponents>(); | |
| winow.Show(); | |
| } | |
| void OnGUI () | |
| { | |
| originalObject = EditorGUILayout.ObjectField("original",originalObject, typeof(GameObject), true) as GameObject; | |
| targetObject = EditorGUILayout.ObjectField("target", targetObject, typeof(GameObject), true) as GameObject; | |
| if( GUILayout.Button("Clone") ){ | |
| Copy(originalObject, targetObject); | |
| } | |
| } | |
| void Copy(GameObject original, GameObject target) | |
| { | |
| Component[] originalComponents = original.GetComponents<Component>(); | |
| for(int i=0; i<originalComponents.Length; i++){ | |
| Component component = originalComponents[i]; | |
| var targetComponent = target.GetComponent(component.GetType() ); | |
| ComponentUtility.CopyComponent(component); | |
| if( targetComponent != null ){ | |
| ComponentUtility.PasteComponentValues( targetComponent ); | |
| }else{ | |
| ComponentUtility.PasteComponentAsNew(target); | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment