以下の内容はhttps://kan-kikuchi.hatenablog.com/entry/UnityEditorInternal_ComponentUtilityより取得しました。


コンポーネントやその値のコピーとペーストをプログラムから行う方法【Unity】【エディタ拡張】





この記事でのバージョン
Unity 2021.3.4f1


はじめに

UnityではInspectorの⁝からコンポーネントをコピーしたりペーストしたり出来ますが、


今回はこれをプログラムから行う方法の紹介です!


UnityEditorInternal.ComponentUtility

コンポーネント関連の操作はUnityEditorInternalのComponentUtilityというクラスを使います。

なお、UnityEditorInternalという名前から分かるようにエディタ専用です。


実際に使ってみると以下のようにそれぞれメソッドを一つ実行するだけ。

//コンポーネントの値をコピー
UnityEditorInternal.ComponentUtility.CopyComponent(transform);

//コピーしたコンポーネントの値を上書き
UnityEditorInternal.ComponentUtility.PasteComponentValues(transform);

//コピーしたコンポーネントを新たに追加
UnityEditorInternal.ComponentUtility.PasteComponentAsNew(gameObject);


ちなみにコンポーネントの移動も出来ますし、

//コンポーネントを一つ上げる
UnityEditorInternal.ComponentUtility.MoveComponentUp(transform);

//コンポーネントを一つ下げる
UnityEditorInternal.ComponentUtility.MoveComponentDown(transform);


DestroyComponentsMatchingやReplaceComponentsIfDifferentといった

Inspectorからは出来ない処理も用意されています。






以上の内容はhttps://kan-kikuchi.hatenablog.com/entry/UnityEditorInternal_ComponentUtilityより取得しました。
このページはhttp://font.textar.tv/のウェブフォントを使用してます

不具合報告/要望等はこちらへお願いします。
モバイルやる夫Viewer Ver0.14