유니티/에디터

오브젝트 컴포넌트 복사및 붙여넣기 (Component Copy and Paste)

송호정 2022. 1. 7. 11:45
반응형

 

 [MenuItem("GameObject/FieldObject/CopyComponet (Ctrl+Alt+Shift+C) %#&c")]
    static void CopyComponent()
    {
        _copyObject = Selection.activeObject as GameObject;
    }

    [MenuItem("GameObject/FieldObject/PasteComponet (Ctrl+Alt+Shift+V) %#&v")]
    static void PasteComponet()
    {
        if (_copyObject == null)
        {
            Debug.LogError("복사된 오브젝트가 없습니다.");
            return;
        }

        Component[] componentList = _copyObject.GetComponents<Component>();

        GameObject obj = Selection.activeObject as GameObject;
        obj.name = _copyObject.name;
        for (int i = 0, count = componentList.Length; i < count; ++i)
        {
            Component targetObjectComponent = obj.GetComponent(componentList[i].GetType());
            UnityEditorInternal.ComponentUtility.CopyComponent(componentList[i]);
            if (targetObjectComponent != null)
            {
                UnityEditorInternal.ComponentUtility.PasteComponentValues(targetObjectComponent);
            }
            else
            {
                UnityEditorInternal.ComponentUtility.PasteComponentAsNew(obj);
            }
        }
    }
반응형