Skip to content

Instantly share code, notes, and snippets.

@tsubaki
Created September 11, 2015 06:28
Show Gist options
  • Select an option

  • Save tsubaki/ca08811a3d332939f10d to your computer and use it in GitHub Desktop.

Select an option

Save tsubaki/ca08811a3d332939f10d to your computer and use it in GitHub Desktop.
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