Skip to main content

Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
TagsGame Engines

tested

ComponentCopyTool .cs

component copier for unity3D  

[needs to be put in Editor folder]

<quote>

using UnityEngine;

using UnityEditor;

public class ComponentCopyTool : EditorWindow

{

    Transform source;

    Transform target;

    [MenuItem("Tools/Component Copy Tool")]

    static void Init()

    {

        GetWindow<ComponentCopyTool>("Component Copier");

    }

    void OnGUI()

    {

        GUILayout.Label("Copy Components", EditorStyles.boldLabel);

        source = (Transform)EditorGUILayout.ObjectField("Source", source, typeof(Transform), true);

        target = (Transform)EditorGUILayout.ObjectField("Target", target, typeof(Transform), true);

        if (GUILayout.Button("Copy Components"))

        {

            CopyAll();

        }

    }

    void CopyAll()

    {

        if (source == null || target == null)

        {

            Debug.LogError("Assign source and target.");

            return;

        }

        Component[] components = source.GetComponents<Component>();

        foreach (Component comp in components)

        {

            if (comp is Transform)

                continue;

            UnityEditorInternal.ComponentUtility.CopyComponent(comp);

            UnityEditorInternal.ComponentUtility.PasteComponentAsNew(target.gameObject);

        }

        Debug.Log("Copied successfully");

    }

}