バーチャル3Dクリエイター神部まゆみです(*^_^*)
この記事はUnityでコンポーネントを一括コピペするスクリプトを書いたので、それについての記事です。
ChatGPTパイセンに聞いてみる
とりあえず彼(彼女?)に聞けばすぐ教えてくれるでしょう。


神部まゆみ
unityをいじっているのですが、オブジェクトAのコンポーネントをオブジェクトBに一括コピーするスクリプトが欲しいです。

ChatGPT
なるほど!
Unityで「オブジェクトAのコンポーネントをオブジェクトBに一括コピーするエディタ拡張」を作りたいんですね。
以下のスクリプトは、インスペクター上でオブジェクトを指定してボタンを押した時にのみコピーを実行する仕様になっています。
✅ 仕様
- コピー元 (sourceObject)
- コピー先 (targetObject)
- Transform の値を上書きするかどうかチェックボックスを追加
- 同じ種類のコンポーネントがある場合に上書きするかどうかチェックボックスを追加
- 実行ボタンを押すまでコピーしない
コンポーネント一括コピペスクリプト ComponentCopier.cs
using UnityEngine;
using UnityEditor;
using System;
using System.Reflection;
public class ComponentCopier : MonoBehaviour
{
public GameObject sourceObject; // コピー元
public GameObject targetObject; // コピー先
[Header("オプション")]
public bool overwriteTransform = false; // Transformを上書きするか
public bool overwriteExisting = false; // 既存コンポーネントを上書きするか
}
#if UNITY_EDITOR
[CustomEditor(typeof(ComponentCopier))]
public class ComponentCopierEditor : Editor
{
public override void OnInspectorGUI()
{
DrawDefaultInspector();
ComponentCopier copier = (ComponentCopier)target;
if (GUILayout.Button("コンポーネントをコピー"))
{
if (copier.sourceObject == null || copier.targetObject == null)
{
Debug.LogError("コピー元またはコピー先が設定されていません!");
return;
}
CopyComponents(copier);
}
}
private void CopyComponents(ComponentCopier copier)
{
GameObject source = copier.sourceObject;
GameObject target = copier.targetObject;
foreach (var comp in source.GetComponents<Component>())
{
if (comp is Transform)
{
if (copier.overwriteTransform)
{
Undo.RecordObject(target.transform, "Copy Transform");
target.transform.position = source.transform.position;
target.transform.rotation = source.transform.rotation;
target.transform.localScale = source.transform.localScale;
}
continue;
}
Type type = comp.GetType();
Component targetComp = target.GetComponent(type);
if (targetComp != null)
{
if (copier.overwriteExisting)
{
CopyComponentValues(comp, targetComp);
}
}
else
{
Component newComp = Undo.AddComponent(target, type);
CopyComponentValues(comp, newComp);
}
}
Debug.Log("コンポーネントコピー完了!");
}
private void CopyComponentValues(Component source, Component destination)
{
Type type = source.GetType();
BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance;
foreach (var field in type.GetFields(flags))
{
if (field.IsStatic) continue;
field.SetValue(destination, field.GetValue(source));
}
foreach (var prop in type.GetProperties(flags))
{
if (!prop.CanWrite || !prop.CanRead || prop.Name == "name") continue;
try
{
prop.SetValue(destination, prop.GetValue(source, null), null);
}
catch { }
}
}
}
#endif
使い方
- 空の GameObject に ComponentCopier をアタッチする
sourceObject
にコピー元、targetObject
にコピー先を設定する- 必要に応じて Transform上書き や 既存コンポーネント上書き をチェックする
- 「コンポーネントをコピー」ボタンを押すとコピーが実行される
注意点
- **スクリプトや特殊なコンポーネント(例:Animator, Rendererの一部プロパティ)**は完全にコピーできないことがあります(Unityの内部制御があるため)。
- その場合はフィールド値だけのコピーに留まります。
実際に動かしてみる
動作確認のため動かしていきます。
C#ファイルを作ってコピペする
新規C#ファイルを作ってコードをコピペ、保存します。

オブジェクトを二つ用意する
一方はスフィアコライダーだけ追加してあります。
CopyA Transform、Sphereコライダー
CopyB Transformのみ
の状態です。
コピーさせたいオブジェクトにコンポーネントを追加する
CopyBオブジェクトにComponentCopierコンポーネントを追加します。

設定して動かす
Source Objectにコピー元のオブジェクト、ターゲットオブジェクトにコピー先のオブジェクトを指定し、任意のオプションにチェックを入れて「コンポーネントをコピー」ボタンを押せばOK。
無事にコピーできましたね。
おわりに
案外簡単に動いて良かった。
たまにコンポーネントをコピーさせたい時があるから作ってみました。
また何かあれば追記します(*^_^*)