zoukankan      html  css  js  c++  java
  • 复制、粘贴一个物体的所有组件

     1 using UnityEngine;
     2 using UnityEditor;
     3 using System.Collections.Generic;
     4 
     5 public class CopyAllComponent : EditorWindow
     6 {
     7     static Component[] copiedComponents;
     8     [MenuItem("GameObject/Copy Current Components",false,10)]
     9     static void Copy()
    10     {
    11         copiedComponents = Selection.activeGameObject.GetComponents<Component>();
    12     }
    13 
    14     [MenuItem("GameObject/Paste Current Components",false,10)]
    15     static void Paste()
    16     {
    17         foreach (var targetGameObject in Selection.gameObjects)
    18         {
    19             if (!targetGameObject || copiedComponents == null) continue;
    20 
    21             var targetComponents = new List<Component>(targetGameObject.GetComponents<Component>());
    22 
    23             foreach (var copiedComponent in copiedComponents)
    24             {
    25                 if (!copiedComponent) continue;
    26 
    27                 var targetComponent = targetComponents.Find((item) => item.GetType() == copiedComponent.GetType());
    28 
    29                 UnityEditorInternal.ComponentUtility.CopyComponent(copiedComponent);
    30 
    31                 if (targetComponent != null)
    32                 {
    33                     UnityEditorInternal.ComponentUtility.PasteComponentValues(targetComponent);
    34                 }
    35                 else
    36                 {
    37                     UnityEditorInternal.ComponentUtility.PasteComponentAsNew(targetGameObject);
    38                 }
    39             }
    40         }
    41     }
    42 
    43 }
  • 相关阅读:
    近期Android学习II
    近期Android学习
    Java中AQS基本实现原理
    Java中CAS 基本实现原理
    SpringBoot 消息国际化配置
    SpringBoot2.0 配置多数据源
    浅谈Java 线程池原理及使用方式
    Java并发编程之闭锁与栅栏
    Java 8 Stream API实例
    第二阶段考试
  • 原文地址:https://www.cnblogs.com/bzyzhang/p/5517862.html
Copyright © 2011-2022 走看看